codescracker


objective c

Objective-C Pointers



« Previous Tutorial Next Tutorial »


Pointers makes more easy in performing some tasks in Objective-C. And some tasks like dynamic memory allocation can't be performed without using pointers in Objective-C. To become a perfect Objective-C programmer, you must have to learn pointers in Objective-C.

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

Objective-C Pointers Example

Here is an example program, demonstrating pointers in Objective-C. This Objective-C program will print the address of the declared variable.

/* Objective-C Pointers - Example Program */
		
#import <Foundation/Foundation.h>
int main()
{
	int  var1;
	char var2[10];
	NSLog(@"Address of var1 = %x\n", &var1);
	NSLog(@"Address of var2 = %x\n", &var2);
	return 0;
}

When the above code is compile and executed, it will produce the following result:

2014-08-03 13:18:37.932 demo[17552] Address of var1 = 1c0843fc
2014-08-03 13:18:37.932 demo[17552] Address of var2 = 1c0843f0

A pointer is simply a variable whose value is the address of another variable, that is, direct address of the memory location. You must have to declare a pointer variable before its use, in storing the address of any variable.

Declare a Pointer Variable in Objective-C

Here is the general form to declare a pointer variable in Objective-C:

type *variable-name;

Here, type is any valid Objective-C data type, is the pointer's base type, and variable-name is the name of the pointer variable. And the asterisk (*) operator must be used before the pointer-variable to declare a pointer variable. Here are examples of some valid pointer declaration in Objective-C:

int *iptr;		/* pointer to an integer */
float *fptr;		/* pointer to a float */
char *cptr;		/* pointer to a character */
double *dptr;		/* pointer to a double */

How to use Pointers in Objective-C ?

To use pointers in Objective-C, follow these three steps:

This is done just by using the unary operator * (you can also called it as memory at address operator) that returns the value of the variable located at the address specified by its operand. Here is an example program, illustrating this concept of pointer in Objective-C

/* Objective-C Pointers - Example Program */
		
#import <Foundation/Foundation.h>
int main()
{
	int  var = 20;
	int  *iptr;		/* pointer variable declared */
	iptr = &var;		/* address of var variable is assigned to the pointer variable */
	NSLog(@"Address of var = %x\n", &var);
	NSLog(@"Address stored in iptr = %x\n", iptr);
	NSLog(@"Value of *iptr = %d\n", *iptr);
	return 0;
}

When the above code is compile and executed, it will produce the following result:

2014-08-03 13:18:37.932 demo[24179] Address of var = 337ed41c
2014-08-03 13:18:37.932 demo[24179] Address stored in iptr = 337ed41c
2014-08-03 13:18:37.932 demo[24179] Value of *iptr = 20

NULL Pointers in Objective-C

A good Objective-C programmer always assigns a NULL value to a pointer variable in case they don't have any exact address to be assigned. This is simply done at the time of pointer variable declaration. A pointer which is assigned NULL is called as NULL pointer in Objective-C.

Here is an example, demonstrating NULL pointer in Objective-C:

/* Objective-C Pointers - Example Program */
		
#import <Foundation/Foundation.h>
int main()
{
	int *iptr = NULL;
	NSLog(@"The value of iptr = %x\n", iptr);
	return 0;
}

When the above code is compile and executed, it will produce the following output:

2014-08-03 13:18:37.932 demo[28027] The value of iptr = 0

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test