Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c
communitycreator

What are the pointers in objective C?

Fouzia Bashir

Overview

Pointers in objective-C are helpful to complete our programming tasks easily. Some tasks, for example, dynamic memory allocation, cannot be performed without pointers. We must learn pointers to work efficiently as objective-C programmers.

What are pointers?

A pointer is referred to as a variable, and its value is the memory location of another variable. We must declare the pointer before storing any address in it. The basic syntax for declaring a pointer is as follows:

Syntax


type *pointer_name

The type shows the data type of pointer. It must be a valid type of objective-C language. The variable_name shows the name of the pointer variable. We must use an asterisk * while declaring the pointer. The following are some examples regarding pointer declaration:


float  *f1;    /* pointer of type float*/
int    *num1;    /* pointer of type integer */
char   *c1     /* pointer of type character */

The real data type regarding all the pointers of type int, float, or char, and so on. It is a lengthy hexadecimal number that shows the memory address of a variable.

How can we use a pointer?

Let’s discuss some crucial operations that can be done with the help of pointers:

  • Defining a pointer variable.
  • Assigning the memory address regarding variable to the pointer.
  • Accessing the value available at the address in the pointer variable.

Explanation

Here is an example to understand the declaration and working of pointers:

#import <Foundation/Foundation.h>

int main () {
   int  v1 = 10;    /* real declaration of variable*/
   int  *p1;         /* declaring the pointer variable*/  
   p1 = &v1;    /* storing the memory  address of v1 in pointer variable*/

   NSLog(@"The memory address of v1 variable: %x\n", &v1  );
   /* The address stored in the pointer variable */
   NSLog(@"Memory Address stored in p1 variable: %x\n", p1 );
   /* accessing the value by using the pointer */
   NSLog(@"The Value of *p1 variable: %d\n", *p1 );

   return 0;
}

NULL pointers

If we do not have the particular address to be allocated, then a NULL pointer is always a perfect choice in this regard. It must be done when we declare the variable. If the NULL value is assigned to a pointer, it is known as a NULL pointer. Let’s discuss an example regarding it:

#import <Foundation/Foundation.h>

int main () {
   int  *np = NULL;
   NSLog(@"The Pointer's value is : %x\n", np  );
   return 0;
}

Programs cannot access the memory at address 0 in some operating systems because OS may reserve the space or memory. We can also use if statements to check the pointer.

More about pointers

Let’s discuss some more concepts regarding pointers:

  • We can perform three arithmetic operators on pointers. These operators are ++,–, +,-.

  • We can also define an array of the pointer. This array can hold the number of pointers in int.

  • Another concept is pointer to pointer. It means we can have a pointer on a pointer.

  • Passing pointers to the function is another crucial concept and it should be considered while learning about objective-C pointers. We can pass arguments by address or reference to the function. It enables the passed parameters to be altered in calling the function through the called function.

  • Objective-C also permits returning a pointer to a local, static variable. It also returns the dynamically allocated memory location.

RELATED TAGS

c
communitycreator
RELATED COURSES

View all Courses

Keep Exploring