Pointers and Arrays

If you have an array of 100 integers called values, you can define a pointer called valuesPtr, which you can use to access the integers contained in this array with the following statement:

int *valuesPtr;

When you define a pointer that will be used to point to the elements of an array, you don’t designate the pointer as type “pointer to array”; instead, you designate the pointer as pointing to the type of element contained in the array.

If you had an array of Fraction objects called fracts, you could similarly define a pointer to be used to point to elements in fracts with the following statement:

Fraction **fractsPtr;

To set valuesPtr to point to the first element in the values array, you just write this:

valuesPtr = values; ...

Get Programming in Objective-C, Sixth 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.