Open In App

Pointer Arithmetic in Objective-C

Last Updated : 07 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

  1. Increment of a pointer
  2. Decrement of a pointer
  3. Addition of integer to a pointer
  4. 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




// Objective-C program to illustrate pointer to airthmetic 
#import <Foundation/Foundation.h>
int main()
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
      
    // Create an array size of five elements 
    int arr[5] = {59, 43, 32, 24, 13};
      
    // Create a pointer variable with a null initialize
    int *pointer = NULL;
      
    // Pass the first element of the address 
    // in the pointer variable 
    pointer = arr;
    int i;
      
    // Increment the pointer variable
    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




// Objective-C program to illustrate pointer to airthmetic 
#import <Foundation/Foundation.h>
  
// Maximum size
const int MAX = 5;
  
int main()
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
      
    // Creating an array
    int arr[]= {59, 43, 32, 24, 13};
      
    // Creating a pointer
    int *pointer = NULL;
      
    // Assigning the last element of the
    // address in the pointer variable
    pointer = &arr[MAX - 1];
    int i;
      
    // Decreasing the address of the pointer 
    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




// Objective-C program to illustrate pointer to airthmetic 
#import <Foundation/Foundation.h>
   
int main (int argc, const char * argv[])
{
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
          
        // Define a pointer  
        int a = 10, *ptr;
          
        // Assign the address to the ptr 
        ptr = &a;
        
        // Print the address of the pointer variable
        NSLog (@"Address of %x\n", ptr);
         
        // Add 2 in the address of the variable
        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




// Objective-C program to illustrate pointer to airthmetic 
#import <Foundation/Foundation.h>
   
int main (int argc, const char * argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
      
    // Define a pointer  
    int a = 10, *ptr;
      
    // Assign the address to the ptr 
    ptr = &a;
      
    // Print the address of the pointer variable 
    NSLog(@"Address of %x\n", ptr);
      
    // Subtract 2 from the address of the variable
    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




// Objective-C program to traversing the array 
// using pointer variable
#import <Foundation/Foundation.h>
int main()
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int arr[3] = {5, 8, 9}, i;
    int *ptr;
      
    // Assign the first array address to the pointer variable
    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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads