10_Objective-C Pointers

Some Objective-C programming tasks are performed more easily with pointers


Other tasks, such as dynamic memory allocation, cannot be performed without using pointers.

As you know, every variable is a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator, which denotes an address in memory.

Getting the address in memory.

We use ampersand (&) operator, which denotes an address in memory.


What Are Pointers?

A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location.


type *var-name

Here, type is the pointer’s base type; it must be a valid Objective-C data type and var-name is the name of the pointer variable.

The asterisk * you used to declare a pointer is the same asterisk that you use for multiplication.

int    *ip;    /* pointer to an integer */
double *dp;    /* pointer to a double */
float  *fp;    /* pointer to a float */
char   *ch     /* pointer to a character */

The only difference between pointers of different data types is the data type of the variable or constant that the pointer points to.


NULL Pointers in Objective-C

It is always a good practice to assign a NULL value to a pointer variable in case you do not have exact address to be assigned.

This is done at the time of 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.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s