Open In App

Array of Pointers in Objective-C

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

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 memory dynamically which can be free after the complete execution of the program. 

Pointers with arrays stores the starting address of the array. Here array name itself acts as a pointer to the first element of the array and also if a pointer variable stores the base address of an array then we can manipulate all the array elements using the pointer variable only. Pointers can be associated with multidimensional arrays (2-D and 3-D arrays) as well because it helps to create the memory dynamically so that stack memory would not be wasted.

Array of Pointers 

When we require multiple pointers in a program we create an array of multiple pointers and use it in the program. An array of pointers is also known as a pointer array.  An array of pointers is a pointer variable that consists of multiple values of the pointer type to form an array and they are stored dynamically rather than stored in stack memory. It means that only one pointer variable is created which points to the array elements stored in other memory by pointing toward the address of those array elements. There may be a situation when we want to maintain an array, which can store pointers to an integer or character or any other data type available. It is basically used to store the elements dynamically to save the memory and not to waste the stack memory and once the use of that array is done then we can free that dynamic memory also.

Syntax:

<data_type> *<pointer_name>[<size>];

Example of declaring an array of point of integers –

int *ptr[MAX];

We can also use an array of pointers for integers as well as for characters to store a list of strings.

Before understanding the concept of arrays of pointers, let us understand how a normal array of integers is created and how they are accessed − consider we have an array ‘arr’ of size 3 indexing starting from 0 to 2, and elements are 1, 5 and 7 and size of one integer is 4 bytes and if we create an array of integer of size 3, then it will take 12 bytes to store the elements in memory.

Array of size 3

creating an array of size 3

Now create one pointer variable ‘ptr’ and assign its address using array indexes, and this ptr variable will take 8 bytes to store this pointer variable, and the rest of the memory is used by accessing another memory with the help of the address.

ptr[i] = &arr[i];  /* here ptr[i] assigns the address of array to create pointer of array

Array of pointers

ptr address storing the address of an array

In the above image, it is shown how ‘ptr’ points the array by pointing its address towards the array index

‘ptr[0]’ having address 1004 is pointing the element having index of 0 of ‘arr’

‘ptr[1]’ having address 1008 is pointing the element having index of 1 of ‘arr’

‘ptr[2]’ having address 1012 is pointing the element having index of 2 of ‘arr’

and now if we print ptr using “*ptr[i]” it will print the element present at ith indexing address of ptr.

Example 1:

The following program consists of an array of size 3 and its element, after that we will create a pointer ‘ptr’ and store the array index into the pointer address and this will help to point the element using its address, now consider the below example for practical understanding:

ObjectiveC




// Objective-C program of array of pointer
#import <Foundation/Foundation.h>
   
int main () 
{
    int  var[] = {1, 5, 7};
    int i, *ptr[3];
      
    for(i = 0; i < 3; i++) 
    {
        /* Assign the address of integer */
        ptr[i] = &var[i];   
    }
    for(i = 0; i < 3; i++) 
    {
      
        /* Print the value in ptr */
        NSLog(@"Value of var[%d] having pointer address of %d = %d\n", i, ptr[i], *ptr[i]); 
    }
    return 0;
}


Output:

Value of var[0] having pointer address of 1913655444 = 1
Value of var[1] having pointer address of 1913655448 = 5
Value of var[2] having pointer address of 1913655452 = 7

Example 2:

Following is the program in which we have an array of pointers containing strings as elements and we will print this array of pointers using array indexes.

ObjectiveC




// Objective-C program of array of pointer
#import <Foundation/Foundation.h>
  
int main () 
{
  
    /* names pointer is assigned to have its values */
    char *names[] = {"Mona", "Suman", "Ali", "Soma"};   
      
    int i;
    for(i = 0; i < 4; i++) 
    {
      
        /* print the all values of pointer names */
        NSLog(@"Value of names[%d] = %s\n", i, names[i]);   
    }
    return 0;
}


Output:

Value of names[0] = Mona
Value of names[1] = Suman
Value of names[2] = Ali
Value of names[3] = Soma
 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads