This article deals with how pointers can be used along with functions. Two main usage are covered
- Pointers passed as parameter to function.
- Pointers returned from function.
Pointers passed as parameter to function
Best way to explain this is by taking example of most popular example. Write a function that would swap values in two integer variables.Strategy:
- Our function that swaps two variables be named swap.
- Since it would swap(exchange) two integer variables lets it prototype be as follows
- void swap(int, int);
- Swapping would be done by taking temporary variable
#include <stdio.h> void swap(int, int); int main() { int a = 10, b = 30; printf("Before Swap: a is %d and b is %d\n", a, b); swap(a, b); printf("After Swap: a is %d and b is %d\n", a, b); } void swap(int x, int y) { int temp; temp = x; x = y; y = temp; return; }
Before Swap: a is 10 and b is 30 After Swap: a is 10 and b is 30
Hence the above program is swapping the local variables of function swap. Now how do we swap a and b ? Answer is using pointers. Instead of passing the value of a and b to swap. We need to pass the address of a and b. So that using the address of a and b swap function could swap the values in variable.
New Strategy
- Let the function name be still swap.
- Since it would swap(exchange) two integer variables lets it prototype be as follows
- void swap(int *, int*);
- Swapping would be done by taking temporary variable
#include <stdio.h> void swap(int *, int *); int main() { int a = 10, b = 30; printf("Before Swap: a is %d and b is %d\n", a, b); swap(&a, &b); printf("After Swap: a is %d and b is %d\n", a, b); } void swap(int *px, int *py) { int temp; temp = *px; *px = *py; *py = temp; return; }
Before Swap: a is 10 and b is 30 After Swap: a is 30 and b is 10
- Hence using pointers functions can access the variables of calling function.
- Note how in function call address was passed using the address operator &.
- Pointers are named generally with p prefixed to them, Just so that programmer is aware that it is a pointer.
Pointers returned from function
Returning pointer from the function is very tricky part. Let me give a negative example here, example which should not be followed#include <stdio.h> int *fun(); int main() { int *pa; pa = fun(); printf("Value of a inside main is %d\n", a); *pa = 200; printf("Value of a inside main, after assigment is %d\n", *pa); return 0; } int *fun() { int a; a = 100; printf("Value of a inside fun is %d\n", a); return &a; }
$ gcc a.c a.c: In function 'fun': a.c:18:2: warning: function returns address of local variable [enabled by default] $ ./a.out Value of a inside fun is 100 Value of a inside main is 100 Value of a inside main, after assigment is 200
#include <stdio.h> int *fun(); void funx(); int main() { int *pa; pa = fun(); printf("Value of a inside main is %d\n", *pa); *pa = 200; printf("Value of a inside main, after assigment is %d\n", *pa); funx(); printf("Value of a inside main, after funx call is %d\n", *pa); return 0; } int *fun() { int a; a = 100; printf("Value of a inside fun is %d\n", a); return &a; } void funx() { int g = 1000; printf("Inside funx, value of g is %d\n", g); return ; }
Value of a inside fun is 100 Value of a inside main is 100 Value of a inside main, after assigment is 200 Inside funx, value of g is 1000 Value of a inside main, after funx call is 1000
a.c:18:2: warning: function returns address of local variable [enabled by default]
Most of others (programmers of non C/C++ language) would give above example in proving C is unsafe. My take on this is
- C allows programmer to do mistake. Some times mistake would result in catastrophic result.
- But if you know how C works, you would not do the mistake in first place.
- And there was warning already issued by compiler, hence don't complain.
As much as knowing what works is important, also knowing what doesn't work and why it doesn't work is more important.
Links
Next Article - C Programming #46: Pointer to functionPrevious Article - C Programming #44: Pointer - other operators
All Article - C Programming
No comments :
Post a Comment