Pointers

If you work with C, or C++, or Objective-C (or C#, boo, hiss) you work with pointers. Today, I want to argue that the way everyone else declares and thinks about pointers is wrong, wrong, wrong!

Syntactics

At bottom, this is a syntactic issue, and a pretty simple one. Most code declares pointer variables like this:

int *intPtr;
char *charPtr;
void *voidPtr;

In fact, this arrangement of whitespace is (mostly) optional, and the following syntax is equally valid:

int* intPtr;
char* charPtr;
void* voidPtr;

Semantics

It seems to me that these two, very subtly different, styles of notation are understood in rather different ways. To begin with, the expression:

int foo;

says, plainly, that the variable foo is of type int.

By analogy, the expression:

int *intPtr;

says that the expression *intPtr is of type int. It falls to the programmer to work out that if a dereferenced intPtr is an int, then intPtr must itself be a pointer to an int.

On the other hand, the expression:

int* intPtr;

directly declares that the variable intPtr is of type int*, or “int-pointer”.

Pragmatics

Is this a small difference? Yes! However, programming is confusing enough (and pointers are notoriously difficult for novices) and I see no reason to introduce unnecessary semantic hiccups into the mix.

Caveats

There is one drawback to the convention I propose. With the standard convention, you can do things like this:

int *foo, *bar, *baz;

which declares three int pointers. If you try to do the same thing with my notation:

int* foo, bar, baz;

you’d declare one int pointer (foo) and two ints (bar and baz).
This doesn’t bother me, but it might bother you.

Share and Enjoy:
  • Twitter
  • Facebook
  • Digg
  • Reddit
  • HackerNews
  • del.icio.us
  • Google Bookmarks
  • Slashdot
This entry was posted in iPhone. Bookmark the permalink.

Comments are closed.