O'Reilly logo

C Programming: Visual Quickstart Guide by Marc Liyanage, Larry Ullman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required