Concept
How do pointers pass values by reference in C?
ComputerScienceOne / Function Pointers
"int *ptrC = &c;\n\nsum(x, y, ptrC);\n\n//at this point c contains the value 30\n\nswap(&x, &y);\n\n//at this point, the values in x and y have been swapped\n\n// x contains 20 and y contains 10\n\nThis should look familiar. We saw this same syntax when we used scanf() to read input from the standard input. We needed to place an ampersand in front of each variable in order to pass the variable by reference so that scanf() could place the results into the respective memory locations. If the variables had been passed by value, then scanf() would not have been able to manipulate their values. You can also specify functions to return pointers which we discuss in detail in Chapter 20."
Related Ideas
- How do you invoke functions that accept pointer parameters?ComputerScienceOne · Passing By Reference
- How can pointers be used to pass variables by reference?ComputerScienceOne · Passing By Reference
- How can functions be assigned to variables and invoked as callbacks?ComputerScienceOne · Function Pointers
- What topics are covered in error handling?ComputerScienceOne · Function Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . .
- What is a function pointer and how is it declared?ComputerScienceOne · Function Pointers