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
#import <Foundation/Foundation.h>
int main()
{
NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init];
int a = 50;
int *ptr;
ptr = &a;
NSLog ( @"Address of variable a = %x\n" , &ptr);
NSLog ( @"Address stored in the pointer ptr: %x\n " , ptr);
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
#import <Foundation/Foundation.h>
int main()
{
NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init];
int arr[5] = {59, 43, 32, 24, 13};
int *pointer;
pointer = arr;
int i;
for (i = 0; i < 5; i++)
{
NSLog ( @"Pointer Arithmetic = %d\n" , *pointer);
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
#import <Foundation/Foundation.h>
const int MAXI = 5;
int main()
{
NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init];
int arr[] = {59, 43, 32, 24, 13};
int *pointer;
pointer = &arr[MAXI-1];
int i;
for (i = MAXI; i > 0; i--)
{
NSLog ( @"Pointer Arithmetic = %d\n" , *pointer);
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
#import <Foundation/Foundation.h>
int main()
{
NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init];
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
Similar Reads
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
Properties in Objective-C
The object-oriented programming language Objective-C is largely used to create applications for Apple's macOS and iOS platforms. It incorporates all of the characteristics of C while also providing more features for object-oriented programming, making it a superset of the C programming language. The
5 min read
Operators in Objective-C
The Objective-C language is developed on top of C Programming. The operator in Objective-C is the same as the C language operators. It is primarily used in developing iOS and MacOS operating systems and software applications for iOS. Operators are used to forming a mathematical expression using vari
11 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
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
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
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
Preprocessors in Objective-C
Preprocessors help the software experts in tasks like Reusability and Conditional compilation. This article focuses on discussing Preprocessors in Objective-C. What is Objective-C?The Objective-C language is a general-purpose, dynamic, and high-level programming language designed to enable object-or
6 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
Variables in Objective-C
A variable is a place for data storage that our programs can access or manipulate. In Objective-C, variables have a defined type that specifies their shape and layout. They also cover the gamut of values and operations that we are capable of performing. Before moving further, we understand the guide
5 min read