Pointer to Pointer in Objective-C
Last Updated :
28 Apr, 2025
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 of another pointer. The pointer-to-pointer concept is often used in situations where you need to pass a pointer as an argument to a function or to store a dynamically allocated object.
There are three types of pointers in Objective-C: a regular pointer, a pointer to constant, and a constant pointer. A regular pointer holds the address of a variable, while a pointer to a constant holds the address of a constant value. A constant pointer holds a constant address and cannot be changed.
Pointer to Pointer
A pointer to pointer, or double pointer, is a type of pointer that holds the address of another pointer. A double pointer is declared using two asterisks (**) in the declaration, such as int p, where p is a double pointer. The first asterisk (*) represents the type of data stored at the address held by the pointer, and the second asterisk () represents that it is a pointer to a pointer.
Syntax:
The syntax for declaring a pointer-to-pointer is similar to that of a regular pointer, with two asterisks used to indicate that it is a pointer to a pointer.
data_type **pointer_name;
Example: int **p;
Here, pointer to pointer includes &, *, and **. The & operator is used to get the address of a variable, while the * operator is used to access the value stored at the address held by the pointer. The ** operator is used to access the value stored at the address held by a pointer to a pointer.
Example 1:
ObjectiveC
// Objective-C Program to illustrate pointer to
// pointer to pass a pointer as an argument to a function
#import <Foundation/Foundation.h>
// Function that takes two pointer to pointers
// as arguments and swaps their values
void swap(int **p, int **q)
{
// Store the value of *p in a temporary variable
int *temp = *p;
// Assign the value of *q to *p
*p = *q;
// Assign the value of temp to *q
*q = temp;
}
int main(int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Declare two integers
int a = 10, b = 20;
// Declare two pointers to hold
// the addresses of a and b
int *p = &a, *q = &b
NSLog(@"Before Swapping");
NSLog(@"Value of a = %d", a);
NSLog(@"Value of b = %d", b);
// Call the swap function, passing
// the addresses of p and q
swap(&p, &q);
NSLog(@"\nAfter Swapping");
NSLog(@"Value of a = %d", *p);
NSLog(@"Value of b = %d", *q);
[pool drain];
return 0;
}
Output:
Before Swapping
Value of a = 10
Value of b = 20
After Swapping
Value of a = 20
Value of b = 10
Example 2: Using pointer to pointer to dynamically allocate memory.
ObjectiveC
// Objective-C Program to illustrate pointer
// to pointer to dynamically allocate memory.
#import <Foundation/Foundation.h>
int main()
{
// Integer variable
int i = 5;
// Pointer to an integer
int *p;
// Pointer to a pointer to an integer
int **q;
// Store the address of i in p
p = &i
// Store the address of p in q
q = &p
NSLog(@"Value stored at i = %d", i);
NSLog(@"Value stored at *p = %d", *p);
NSLog(@"Value stored at **q = %d", **q);
return 0;
}
Output:
Value stored at i = 5
Value stored at *p = 5
Value stored at **q = 5
Similar Reads
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
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 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
C++ Pointer To Pointer (Double Pointer) In C++ a Pointer is a variable that is used to store the memory address of other variables. It is a variable that points to a data type (like int or string) of the same type and is created with the * operator. Syntax of a Pointer in C++: data_type_of_pointer *name_of_variable = & normal_variable
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
Array of Pointers in Objective-C 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 m
5 min read