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 ...
Get C Primer Plus, Fourth Edition 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.