Array of Pointers in Objective-C
Last Updated :
28 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
// 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
Similar Reads
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response
In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Backpropagation in Neural Network
Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Polymorphism in Java
Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
3-Phase Inverter
An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
What is Vacuum Circuit Breaker?
A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
AVL Tree Data Structure
An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read
What is a Neural Network?
Neural networks are machine learning models that mimic the complex functions of the human brain. These models consist of interconnected nodes or neurons that process data, learn patterns, and enable tasks such as pattern recognition and decision-making.In this article, we will explore the fundamenta
14 min read