Open In App

Pointers in Objective-C

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

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 valid data type. They are generally used for dynamic memory allocation, without pointers we cannot allocate memory dynamically. 

Syntax:

type *var-name;

Here type represents the data type of the pointer and it must be a valid type. Var-name represents the name of the variable and an asterisk(*) is used to declare a pointer.

Example:

int *ptr;

float *number;

char *mychar;

How to use Pointers?

To use Pointers in Objective-C. We need to follow the following steps:

Step 1: Declare a pointer. To use a pointer first we declare a pointer with a valid name and data type.

Syntax:

type *var-name;

Example:

int *myPtr;

Step 2: Assign the memory to the pointer. To use a pointer we need to assign the address of another variable to the pointer variable. Here we use the & (ampersand) unary operator that returns the address of the variable. For example &x gives the address of x. The actual address of the variable is always written in hexadecimal. 

Syntax:

pointer_variable = &var_name

Example:

myPtr = &x1

Step 3: Access the Pointer Value. To access the value in the address we use the unary (*) asterisk operator that returns the value of the variable located at the address specified by its operand.

Syntax:

*var_name

Example:

NSLog(@”pointer Value is %d”, *myPtr) 

So the working example of the pointer is:

Example:

ObjectiveC




// Objective-C program to illustrate pointer 
#import <Foundation/Foundation.h>
  
int main()
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
      
    // Declare a variable
    int a = 50;
      
    // Declare a pointer
    int *ptr;
      
    // Store the address of a in the pointer
    ptr = &a;
      
    // Address of the variable a
    NSLog(@"Address of variable a = %x\n", &ptr);
      
    // Address stored in the pointer
    NSLog(@"Address stored in the pointer ptr: %x\n ", ptr);
      
    // accessing the value using pointer
    NSLog(@"Value of *ptr: %d\n ", *ptr);
      
    [pool drain];
    return 0;
}


Output:

Address of variable a = 78fc7ae0
Address stored in the pointer ptr: 78fc7adc
Value of *ptr: 50

Pointer Arithmetic 

Pointers also do some arithmetic operations but they are very limited. Pointer arithmetic is slightly different from the ones that we generally use mathematically. The pointer operations are:

1. Addition to a pointer: We can add a value to the pointer using the + operator.

Syntax:

myPtr+value

2. Subtraction of a pointer: We can subtract a value from the pointer using the – operator.

Syntax:

myPtr-value

3. Increment: When a pointer is incremented, then it actually increments by the number equal to the size of the data type for which it is the pointer. It comes under in addition. For example, we have a pointer named myPtr which points to the address 2000. After the increment(myPtr++), the address of the pointer is 2004 because each time myPtr is incremented, and points to the next integer address that is 4 bytes next to the current address. Due to the increment operation, the pointer moves to the next address without impacting the actual value at the memory location. 

Syntax:

pointer_name++

Example:

ObjectiveC




// Objective-C program to illustrate Pointer Arithmetic 
#import <Foundation/Foundation.h>
int main()
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
      
    // Declare an array
    int arr[5] = {59, 43, 32, 24, 13};
      
    // Declare a pointer
    int *pointer;
      
    // Assign the arr[0] address to pointer variable
    pointer = arr;
      
    int i;
      
    for(i = 0; i < 5; i++)
    {
        NSLog(@"Pointer Arithmetic = %d\n", *pointer);
          
        // Increment pointer by 1 
        pointer++;
    }
    [pool drain];
    return 0;
}


Output:

Pointer Arithmetic = 59
Pointer Arithmetic = 43
Pointer Arithmetic = 32
Pointer Arithmetic = 24
Pointer Arithmetic = 13

4. Decrement:  When a pointer is decremented, then it actually decrements by the number equal to the size of the data type for which it is the pointer. It comes under subtraction. 

Syntax:

pointer_name–

Example:

ObjectiveC




// Objective-C program to illustrate Pointer Arithmetic 
#import <Foundation/Foundation.h>
const int MAXI = 5;
int main()
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
      
    // Declare an array
    int arr[] = {59, 43, 32, 24, 13};
      
    // Declare a pointer
    int *pointer;
      
    // Assign the arr[0] address to pointer variable
    pointer = &arr[MAXI-1];
      
    int i;
      
    for(i = MAXI; i > 0; i--)
    {
        NSLog(@"Pointer Arithmetic = %d\n", *pointer);
          
        // Decrement pointer by 1 
        pointer--;
    }
    [pool drain];
    return 0;
}


Output:

Pointer Arithmetic = 13
Pointer Arithmetic = 24
Pointer Arithmetic = 32
Pointer Arithmetic = 43
Pointer Arithmetic = 59

NULL Pointers

Null pointers can be assigned the 0 value or NULL. This type of pointer is useful when you declare no address in the pointer. The NULL value is generally assigned to a pointer at the time of declaration. So, if a NULL value is assigned to a pointer, then that pointer is known as a NULL pointer.

Syntax:

int *ptr = NULL ;

Example:

ObjectiveC




// Objective-C program to illustrate NULL Pointer 
#import <Foundation/Foundation.h>
int main()
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
      
    // Creating NULL pointer
    int *myptr  = NULL;
      
    NSLog(@"The value of pointer  = %x\n", myptr);
      
    [pool drain];
    return 0;
}


Output:

The value of pointer = 0

Advantages of Pointers

  • Pointers can be used for dynamic memory allocation and deallocation.
  • Pointers are useful for accessing memory locations.
  • Pointers are useful for working with data structures like Linked Lists, arrays, trees, etc.

Disadvantages of Pointers

  • Pointers are complex to understand
  • Pointers are major for memory leaks
  • Memory corruption occurs if you assign wrong values in the pointer


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads