Clarify concepts, rename a function (#20580)
This commit is contained in:
committed by
Christopher McCormack
parent
957a713aac
commit
538cab1134
@@ -3,13 +3,14 @@ title: Passing pointers to funtions
|
|||||||
---
|
---
|
||||||
|
|
||||||
# Passing pointers to funtions
|
# Passing pointers to funtions
|
||||||
C allows passing a pointer to a function. To achieve this, simply declare the parameters as pointer type.
|
C allows passing a pointer to a function. This is useful when you want to modify variables that are out of the scope of that function.
|
||||||
This way of passing references to functions is useful when you want to modify variables that are out of the scope of that function.
|
|
||||||
|
Let's look at an example where using variables alone won't work:
|
||||||
|
|
||||||
```C
|
```C
|
||||||
// incorrect implementation of swap
|
// incorrect implementation of swap
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
void swap(int a, int b){
|
void noswap(int a, int b){
|
||||||
int c;
|
int c;
|
||||||
c = a;
|
c = a;
|
||||||
a = b;
|
a = b;
|
||||||
@@ -23,7 +24,10 @@ int main(){
|
|||||||
printf("Value of var2: %d \n", var2); // prints 20
|
printf("Value of var2: %d \n", var2); // prints 20
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
In this code example the swap function does not work as intended since it swaps two variables that exist only inside the scope of that function. To fix this we make a modification as shown below.
|
In this code example the swap function does not work as intended since it swaps two variables that exist only inside the scope of that function. The function allocates space on the stack and performs the swap within the scope of the function. However, the swap is promptly forgotten once the function is complete and the space on the stack is freed without updating the global variables.
|
||||||
|
|
||||||
|
To solve this, simply declare the parameters as pointers to the global variables:
|
||||||
|
|
||||||
```C
|
```C
|
||||||
// correct implementation of swap
|
// correct implementation of swap
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@@ -40,4 +44,7 @@ int main(){
|
|||||||
printf("Value of var2: %d \n", var2); // prints 10
|
printf("Value of var2: %d \n", var2); // prints 10
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
In the second code example you were able to change the values of the variables only because you were constantly de-referencing a pointer within the function instead of trying to change the values directly
|
|
||||||
|
In the second code example you were able to change the values of the variables only because you were constantly de-referencing pointers within the function, changing the data directly in memory, instead of in the function's temporary frame on the stack.
|
||||||
|
|
||||||
|
Notice the use of `*` to denote a pointer to a piece of memory and the use of `&` to denote the address of a piece of memory.
|
||||||
|
Reference in New Issue
Block a user