[an error occurred while processing this directive] [an error occurred while processing this directive]
CS137 - Programming Principles

int *p or int* p

As a knowledgeable software engineer, you may be aware that there are two ways to declare a pointer in C.

```    int *p;

int* p;
```

In both cases, p is a pointer to an integer. They mean the exact same thing. The compiler treats them the exact same way.

Use int *p

At this point, most tutors will tell you, "Just pick the one you like. It doesn't make a difference anyway." And indeed, most people will use the one that they are first taught with.

I, however, believe that the first notation is much more superior than the second. That is, you should put the star next to your variable name. After reading this, I hope that you will start using the first notation a little more. Allow me to show you why.

(1) It's a good habit

Let me give you a little quiz. Suppose we have the following code:

```    int* a, b, c;
```

What are the types of a, b, and c? To make it easier for you, I'll make it multiple choice.

 a is a b is a c is a A) pointer pointer pointer B) pointer integer integer

I encourage you to answer this one without a compiler. The answer is [B]. (Highlight the brackets to see the answer.)

If you have the habit of attaching the star next to the variable, like this:

```    int *a, b, c;
```

you are much less likely to get this one wrong.

(2) You shouldn't read from left to right

Another reason why some people put their stars next to the data type is that they have the habit of reading declarations from left to right.

```    int* p;
```

So, this is an integer pointer, and its name is 'p'. This is so intuitive, right?

If this is how you read these declarations, let me tell you that declarations are not supposed to be read this way. I will illustrate this point with another example. The following is an actual declaration from <signal.h>

```void (*signal(int sig, void (*func)(int)))(int);
```

What is signal? Try to read this one yourself without scrolling further down.

No, seriously. Try it yourself first.

If you realize that you have no idea, then let me walk you through it.

```void (*signal(int sig, void (*func)(int)))(int);
```
'signal' is a...
```void (*signal(int sig, void (*func)(int)))(int);
```
... function that ...
```void (*signal(int sig, void (*func)(int)))(int);
```
takes two parameters.
```void (*signal(int sig, void (*func)(int)))(int);
```
The first parameter is called 'sig', it is an integer.
```void (*signal(int sig, void (*func)(int)))(int);
```
The second parameter is called 'func', it is a...
```void (*signal(int sig, void (*func)(int)))(int);
```
... pointer to ...
```void (*signal(int sig, void (*func)(int)))(int);
```
... a function that takes in an integer
```void (*signal(int sig, void (*func)(int)))(int);
```
... and returns nothing.
```void (*signal(int sig, void (*func)(int)))(int);
```
The signal function returns...
```void (*signal(int sig, void (*func)(int)))(int);
```
... a pointer to...
```void (*signal(int sig, void (*func)(int)))(int);
```
... a function that takes in an int...
```void (*signal(int sig, void (*func)(int)))(int);
```
... and returns nothing.

Notice how you always start with the variable name, and read the next thing that is attached to it, one by one. You do not start from the data type. Rather, you finish with it.

In my opinion, int *p emphasizes on that idea, int* p doesn't.

That's all the ranting for now

If you have a different opinion, or if you have more reasons why int *p is better, the course tutor is happy to know about it. Here's a newsgroup posting on this subject. Alternatively, you can find the tutor's contact information through the personel page.

-- David, CS 137 Tutor of Fall 2010