Objective C Pointers - Objective C

What are Pointers in Objective-C?

  • Pointers are used in Objective-C programming language to perform some tasks like dynamic memory allocation.
  • Therefore it is necessary to learn pointers because the above-mentioned task is not performed without using pointers.
  • In Pointers, every variable is a memory location and each memory location has its address defined and that can be accessed using ampersand (&) operator, which denotes an address in memory.

The below example prints the address of the variables defined:

On compiling and executing the above code, the following result is produced:

What Are Pointers?

A pointer is a variable which contains the address in the memory of another variable.Pointer must be declared before using it to store any variable address.The below statement shows the declaration of pointer variable.

In the above statement,
type is the pointer's base type that should be valid data type in Objective-C.
var-name is the name of the pointer variable.
The asterisk * is used to designate a variable as a pointer.

The following statements show the declaration of pointers:

The data type of the value of all pointers, whether integers, float, character, and etc. is the same.
But long hexadecimal number represents a memory address.
There is one difference between pointers of different data types is the data type of the variable or constant that the pointer points to.

How to use Pointers?

To use the pointers the follow the rules mentioned below:

  • Define a pointer variable.
  • Assign the address of a variable to a pointer.
  • Then access the value at the address in the pointer variable. This is done by using unary operator * that returns the value of the variable located at the address specified by its operand.

The following example makes use of these operations:

On compiling and executing the above program, the following result is produced:

NULL Pointers in Objective-C

  • If you don’t have exact address to assign to a pointer variable then assign a NULL value at variable declaration.
  • A pointer that is assigned NULL is called a null pointer.
  • The NULL pointer is a constant with a value of zero defined in several standard libraries.


On compiling and executing the above program, the following result is produced:

Some operating systems do not permit to access memory at address 0 because that memory is reserved by the operating system.
And also memory address 0 has special significance; it signals that the pointer is not intended to point to an accessible memory location. But by convention, if a pointer contains the null (zero) value, it is assumed to point to nothing.

Use if statement to check for a null pointer as follows:

Objective-C Pointers in Detail

The below table shows pointers in detail in Objective-C programming language:

Objective C -Pointer arithmetic
There are four arithmetic operators that can be used on pointers: ++, --, +, -
Objective C - Array of pointers
You can define arrays to hold a number of pointers.
Objective C - Pointer to Pointer
Objective-C allows you to have pointer on a pointer and so on.
Passing pointers to functions in Objective C
Passing an argument by reference or by address both enable the passed argument to be changed in the calling function by the called function.
Return pointer from functions in Objective C
Objective-C allows a function to return a pointer to local variable, static variable and dynamically allocated memory as well.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Objective C Topics