Objective-C – Pointer arithmetic
- As explained in main chapter, Objective-C pointer is an address, which is a numeric value.
- Therefore, you can perform arithmetic operations on a pointer just as you can on a numeric value.
- There are four arithmetic operators that can be used on pointers: ++, –, +, and –
To understand pointer arithmetic, let us consider that ptr is an integer pointer, which points to the address 1000. Assuming 32-bit integers, let us perform the following arithmetic operation on the pointer:
Now, after the above operation, the ptr will point to the location 1004 because each time ptr is incremented, it will point to the next integer location, which is 4 bytes next to the current location.
This operation will move the pointer to next memory location without impacting actual value at the memory location.
If ptr points to a character whose address is 1000, then above operation will point to the location 1001 because next character will be available at 1001.
Incrementing a Pointer
We prefer using a pointer in our program instead of an array because the variable pointer can be incremented, unlike the array name which cannot be incremented because it is a constant pointer.
Decrementing a Pointer
The same considerations apply to decrementing a pointer, which decreases its value by the number of bytes of its data type as shown below:
Pointers may be compared by using relational operators, such as ==, <, and >.
If p1 and p2 point to variables that are related to each other, such as elements of the same array, then p1 and p2 can be meaningfully compared.
The following program modifies the previous example one by incrementing the variable pointer so long as the address to which it points is either less than or equal to the address of the last element of the array, which is &var[MAX – 1]:
Objective-C – Array of pointers
You can define arrays to hold a number of pointers.
Before we understand the concept of arrays of pointers, let us consider the following example, which makes use of an array of 3 integers:
There may be a situation when we want to maintain an array, which can store pointers to an int or char or any other data type available. Following is the declaration of an array of pointers to an integer:
This declares ptr as an array of MAX integer pointers.
Thus, each element in ptr now holds a pointer to an int value.
Following example makes use of three integers, which will be stored in an array of pointers as follows:
Objective-C – Pointer to Pointer
Objective-C allows you to have pointer on a pointer and so on.
A pointer to a pointer is a form of multiple indirection or a chain of pointers.
Normally, a pointer contains the address of a variable. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, which points to the location that contains the actual value
A variable that is a pointer to a pointer must be declared as such.
This is done by placing an additional asterisk in front of its name.
For example, following is the declaration to declare a pointer to a pointer of type int:
When a target value is indirectly pointed to by a pointer to a pointer, accessing that value requires that the asterisk operator be applied twice, as is shown below in the example: