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 can pointers be used to pass variables by reference?ComputerScienceOne · Passing By Reference
- How do you invoke functions that accept pointer parameters?ComputerScienceOne · Passing By Reference
- How can you check for a NULL pointer and dereference a valid pointer?ComputerScienceOne · Passing By Reference
- How can pass-by-reference parameters return both roots of a quadratic equation?ComputerScienceOne · Quadratic Roots
- How can the return value communicate errors when roots are passed by reference?ComputerScienceOne · Quadratic Roots
- How do you call a function in C?ComputerScienceOne · Calling Functions
- What capabilities and constraints do functions have in C?ComputerScienceOne · Declaration: Prototypes
- How does PHP pass arguments by reference?ComputerScienceOne · Passing By Reference