O'Reilly logo

C Primer Plus, Fourth Edition by Stephen Prata

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

Functions, Arrays, and Pointers

Suppose you want to write a function that operates on an array. For example, suppose you want a function that returns the sum of the elements of an array. Suppose marbles is the name of an array of int. What would the function call look like? A reasonable guess would be this:

total = sum(marbles); // possible function call

What would the prototype be? Remember, the name of an array is the address of its first element, so the actual argument marbles, being the address of an int, should be assigned to a formal parameter that is a pointer-to-int:

int sum(int * ar);  // corresponding prototype

What information does sum() get from this argument? It gets the address of the first element of the array and it learns ...

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