Passing Addresses to Functions
Now that you have a basic understanding of addresses (albeit thanks to trivial examples), it's time to look at some tasks that cannot be solved without using addresses. The remaining examples will show how useful pointers are.
In this next example, pointers to variables that exist in the main function will be passed to another function. Why is this useful? Consider this example: you want to create a function that swaps the values of two variables. You might be tempted to try a program like Script 9.5.
Script 9.5. Because the swap() function works on copies of the original values, it cannot affect the values of the actual variables defined in the main program.
The problem is that such an application will not work ...
Get C Programming: Visual Quickstart Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.