[go: up one dir, main page]

0% found this document useful (0 votes)
8 views6 pages

Passing Pointers To Functions

Uploaded by

anuhya0611
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Passing Pointers To Functions

Uploaded by

anuhya0611
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Passing pointers to functions

Passing pointers to functions


• Passing the pointers to the function means the memory
location of the variables is passed to the parameters in the
function, and then the operations are performed. The function
definition accepts these addresses using pointers, addresses
are stored using pointers.
Arguments Passing without pointer

• // C program to swap two values


• // without passing pointer to
• // swap function.
• #include <stdio.h>

• void swap(int a, int b)


• {
• int temp = a;
• a = b;
• b = temp;
• }

• // Driver code
• int main()
• {
• int a = 10, b = 20;
• swap(a, b);
• printf("Values after swap function are: %d, %d",
• a, b);
• return 0; Output Values after swap function are: 10,
• } 20
Arguments Passing with pointers
A pointer to a function is passed in this example. As an argument, a pointer is passed
instead of a variable and its address is passed instead of its value. As a result, any
change made by the function using the pointer is permanently stored at the address
of the passed variable. In C, this is referred to as call by reference.
// C program to swap two values
// without passing pointer to
// swap function.
#include <stdio.h>

void swap(int* a, int* b)


{
int temp; Output
temp = *a; Values before swap function are: 10, 20
*a = *b; Values after swap function are: 20, 10
*b = temp;
}

// Driver code
int main()
{
int a = 10, b = 20;
printf("Values before swap function are: %d, %d\n",
a, b);
swap(&a, &b);
printf("Values after swap function are: %d, %d",
a, b);
return 0;
}
Difference between call by value and call by
reference

You might also like