Array of Pointers in Objective-C
Last Updated :
26 Apr, 2025
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.

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

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
#import <Foundation/Foundation.h>
int main ()
{
int var[] = {1, 5, 7};
int i, *ptr[3];
for (i = 0; i < 3; i++)
{
ptr[i] = &var[i];
}
for (i = 0; i < 3; i++)
{
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
#import <Foundation/Foundation.h>
int main ()
{
char *names[] = { "Mona" , "Suman" , "Ali" , "Soma" };
int i;
for (i = 0; i < 4; i++)
{
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
Similar Reads
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
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 Arithmetic in Objective-C
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 integ
5 min read
Arrays in Objective-C
An Array is a data structure that holds similar types of data in contiguous memory. The Objective-C array is a single variable array that is used to store different types of elements in the array. Also, we can access the objective-c array using an index. It is quite similar to the C programming lang
5 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
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
Data Types in Objective-C
In Objective-C, each variable has an associated data type and each data type has different amounts of memory required in a system. A data type is a classification of data that tells the compiler or interpreter how the programmer intends to use the data. Or in other words, a data type represents what
8 min read
Strings in Objective-C
Strings are a fundamental concept in programming, and they are used to represent text in many different applications. In Objective-C, a string is an object that represents a sequence of characters. A string can be created using several built-in classes and methods, which allow you to manipulate the
4 min read
Passing Pointers to Functions in Objective-C
Pointers are a crucial aspect of any programming language and passing pointers to functions in Objective-C is no exception. In Objective-C, pointers are used to pass values between functions and to manipulate memory in a more flexible manner. Pointers can be passed as arguments to functions and can
3 min read
Text and strings Storage in Objective C
Objective-C is an object-oriented programming language widely used to develop programs for iOS and macOS. This article focuses on discussing how text and string data can be stored in Objective-C. Types and Subtypes of Strings Objective-C has two main types of strings - NSString and NSMutableString.
3 min read