Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

# Pointers in Objective-C

### Comparing the Value of Pointers

When comparing the value of two pointers it is important to ensure you are actually performing the intended comparison. As an example, consider the following code snippet:

```int data[2] = { 99, 99 };
int* x = &data[0];
int* y = &data[1];
if (x == y) { NSLog(@"The two values are the same"); }

```

If you expect this to emit the message "The two values are the same" you would be wrong. The statement x == y compares the address of each pointer and, since x and y both point to different elements within the data array, the statement returns NO.

If, instead, we wanted to determine if the values pointed to by each pointer were identical, we would need to dereference both pointers as demonstrated below:

``` if (*x == *y) { NSLog(@"The two values are the same"); }

```

### Indicating the Absence of a Value

Sometimes you will want to detect if a pointer variable is currently pointing at anything of relevance. For this purpose, you will most likely initialize the pointer to one of the special constants NULL or nil, as demonstrated below:

``` int* x = NULL;
NSString* y = nil;

```

Both constants are equivalent to the value 0 and are used to indicate that the pointer is not currently pointing at anything. The Objective-C convention is to use nil when referring to an object while relegating NULL for use with older C-style data types. Initializing the pointer to one of these special values enables a simple if (y != nil) check to determine if the pointer is currently pointing at anything. Since nil is equivalent to the value 0, you may also see this condition simply written as if (!y).

Also be careful not to dereference a NULL pointer. Trying to read or write from a pointer that points to nothing (NULL) will cause an access violation error, which immediately exits your application.

Now that we understand the concept of pointers and how multiple pointers can reference the same object we are ready to communicate with the object. Communicating with an object enables us to interrogate it for details it stores or request the object perform a specific task on our behalf using the information and resources at its disposal.

### Summary

Enhancing the procedural C language to have object-orientated features is essentially what brought Objective-C to life. The benefits of developing applications in an object-orientated manor generally far out weight the extra effort required to learn the various amounts of additional terminology and techniques that object-orientation entails. Chief among the advantages of object-oriented programming is an improved ability to separate a complex application up into a number of smaller and discrete building blocks called classes. Rather than considering a large complex system, the developer's task becomes one of developing multiple smaller systems that combine and build on top of each other to perform tasks far more complex than any one part.

### More Insights

 To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.