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.

Channels ▼


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.


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.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips 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.