Bifalgorithm post · Computer science one · Function Pointers
How do pointers pass values by reference in C?
How do pointers pass values by reference in C? (Computer science one · Function Pointers) — "int *ptrC = &c;\n\nsum(x, y, ptrC);\n\n//at this point c contains…
Ideas in this flowchart
- Passing variables by reference with pointers in C
- The address of c is stored in ptrC: int *ptrC = &c;
- The pointer ptrC is passed to sum(x, y, ptrC).
- After the call, c contains the value 30.
- The addresses of x and y are passed to swap(&x, &y).
- After the call, x contains 20 and y contains 10.
- scanf() uses the same ampersand syntax to receive variables by reference.
- Passing variables by value would prevent scanf() from manipulating their values.
- Functions can also be specified to return pointers.
- Pointer-returning functions are discussed in detail in Chapter 20.