Pointer Arithmetic in Objective-C
Last Updated :
26 Apr, 2025
The pointer stores the address of another variable. So that we can perform the arithmetic operations on a pointer. There are four types of operators that can be used in pointer arithmetic ++,–,+, and -. Let’s consider that the pointer points integer, which points to address 1000, assuming the integer is 32-bits, so now we perform the following arithmetic operation:
ptr++
Example:
int number = 5;
int *ptr;
// Pointer variable address is 1000
pointer = &number;
/ / Here we use arithmetic operator in pointer variable like this
ptr++;
Now, pointer is point to 1004 address in memory
After completing the above operation, ptr points to 1004 because every time the pointer increment, it will point to the next integer location, which is 4 bytes next to the current location. This pointer points to the next location without impacting the actual value. If the pointer points to a character whose address is 1001 then the next location is 1002 because the character is one byte.
Operations with Pointers
- Increment of a pointer
- Decrement of a pointer
- Addition of integer to a pointer
- Subtraction of integer to a pointer
Increment of a Pointer
The increment of a pointer means when a pointer is incremented it increases by the number equal to the size of the data type for which it is a pointer. For example, suppose we have an integer pointer with the address 2000. Now it incremented by 4(size of the integer) so the new address it will point to is 2004.
Example:
ObjectiveC
#import <Foundation/Foundation.h>
int main()
{
NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init];
int arr[5] = {59, 43, 32, 24, 13};
int *pointer = NULL ;
pointer = arr;
int i;
for (i = 0; i < 5; i++)
NSLog ( @"Pointer Arithmetic = %d\n" , *pointer++);
[pool drain];
return 0;
}
|
Output:
Pointer Arithmetic = 59
Pointer Arithmetic = 43
Pointer Arithmetic = 32
Pointer Arithmetic = 24
Pointer Arithmetic = 13
Decrement of a Pointer
The decrement of a pointer means when a pointer is decremented it decreases by the number equal to the size of the data type for which it is a pointer. For example, suppose we have an integer pointer with the address 2000. Now it decremented by 4(size of the integer) so the new address it will point to is 1996.
Example:
ObjectiveC
#import <Foundation/Foundation.h>
const int MAX = 5;
int main()
{
NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init];
int arr[]= {59, 43, 32, 24, 13};
int *pointer = NULL ;
pointer = &arr[MAX - 1];
int i;
for (i = MAX; i > 0; i--)
NSLog ( @"Pointer Arithmetic = %d\n" , *pointer--);
[pool drain];
return 0;
}
|
Output:
Pointer Arithmetic = 13
Pointer Arithmetic = 24
Pointer Arithmetic = 32
Pointer Arithmetic = 43
Pointer Arithmetic = 59
Addition
When we add a pointer to a value, then the value is first multiplied by the size of the data type and then adds to the pointer.
Example:
ObjectiveC
#import <Foundation/Foundation.h>
int main ( int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[ NSAutoreleasePool alloc] init];
int a = 10, *ptr;
ptr = &a;
NSLog ( @"Address of %x\n" , ptr);
ptr = ptr+2;
NSLog ( @"Address of %x\n" , ptr);
[pool drain];
return 0;
}
|
Output:
Address of 8676967c
Address of 86769684
Subtraction
When we subtract a pointer from a value, then the value is first multiplied by the size of the data type and then subtracts from the pointer.
Example:
ObjectiveC
#import <Foundation/Foundation.h>
int main ( int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[ NSAutoreleasePool alloc] init];
int a = 10, *ptr;
ptr = &a;
NSLog ( @"Address of %x\n" , ptr);
ptr = ptr-2;
NSLog ( @"Address of %x\n" , ptr);
[pool drain];
return 0;
}
|
Output:
Address of 112ca1bc
Address of 112ca1b4
Pointer Arithmetic in Array
A pointer is used for contains another variable of addresses. Adding two addresses is useless because there is no idea what it would be pointing to. You can also assign the address of an array to a pointer. An array name acts like a pointer constant. The value of the constant is the first address of the element of the array. For Example: if an array name is arr & arr[0] then you can reference the whole array using arr
Syntax:
int arr[3] = {5,8,9} ;
int *ptr ;
ptr = arr ; // Assigning the address of an array in pointer variable
Example:
ObjectiveC
#import <Foundation/Foundation.h>
int main()
{
NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init];
int arr[3] = {5, 8, 9}, i;
int *ptr;
ptr = arr;
for (i = 0; i < 3; i++)
{
NSLog ( @"The value of pointer = %d" , *(ptr+i));
}
[pool drain];
return 0;
}
|
Output:
The value of pointer = 5
The value of pointer = 8
The value of pointer = 9
Similar Reads
Pointers in Objective-C
In Objective-C, Pointers are the variables that hold the memory address of another variable. You must have declared the pointer variable before its use. The size of the pointer depends on the architecture of the system. The pointer variable can be defined as a char, int, float, double, or any other
5 min read
Pointer to Arrays in Objective-C
In Objective-C, a pointer to an array is a way to store multiple values of the same data type in contiguous memory locations. These arrays can be manipulated and accessed using pointers, which are variables that store the memory address of another variable. In Objective-C, pointers to arrays are use
4 min read
Array of Pointers in Objective-C
A pointer is a variable that stores the address of another variable. We use pointers because, with the help of pointers the memory is accessed efficiently, it saves memory space, and execution time is faster as compared to the use of normal variables using stack memory because pointers store their m
5 min read
C++ Pointer Arithmetic
In C++, pointer arithmetic means performing arithmetic operations on pointers. It refers to the operations that are valid to perform on pointers. Following are the arithmetic operations valid on pointers in C++: Table of Content Incrementing and Decrementing Pointer in C++Addition of Constant to Poi
7 min read
Pointer to Pointer in Objective-C
Pointers in Objective-C are a powerful and essential concept for any programmer to master. Pointers allow you to manipulate data stored in memory directly and are used to store the address of a variable. Pointer-to-pointer also known as a double pointer, is a type of pointer that holds the address o
4 min read
What are the Protocols in Objective-C?
Objective-C is a language versatile and sophisticated enough for iOS and Mac OS GUI programming. One Objective-C feature that can be called both flexible and dynamic is the use of protocols. Protocols help in the creation of interfaces where a class implementation needs to follow a set of methods an
12 min read
Properties in Objective-C
The object-oriented programming language Objective-C is largely used to create applications for Apple's macOS and iOS platforms. It incorporates all of the characteristics of C while also providing more features for object-oriented programming, making it a superset of the C programming language. The
5 min read
Return Pointer From Functions in Objective-C
A pointer is like a variable or constant that stores the address of another variable. It must be declared before the address value of a variable is to be stored in it. The main advantage of using a pointer is that it frees up the program memory and provides direct access to memory locations. It also
5 min read
Pointers to Structures in Objective C
A pointer is a variable whose value is the address of another variable, e.g., stores the address of the memory location. Like any variable or constant, you must declare a pointer before you can use it to store variable addresses. It simplifies the programs and reduces their length. It is useful for
3 min read
Application of Pointer in C++
In C++, pointer is a variable that stores the memory address as its value. It is a powerful feature that forms the backbone of lower-level memory manipulation in C++. It is used in a variety of applications. Some of the common applications of pointers are discussed in this article. 1. Pass Arguments
4 min read