0

I have seen,

int *pnum;
int * pnum;
int* pnum;

Are these all the same?

amalloy
  • 89,153
  • 8
  • 140
  • 205
johnny
  • 19,272
  • 52
  • 157
  • 259
  • Which is itself a duplicate of at least a dozen other questions ;) – Martin J. Mar 07 '14 at 04:16
  • @MartinJ. It didn't show up, and I couldn't find it on Google. I don't think I used the right search terms. – johnny Mar 07 '14 at 04:17
  • @johnny: As noted in the accepted answer, that's because this isn't about referencing. Searching about "pointer declaration" would have gotten you some answers. But the point of my comment was that you aren't the only one to make this mistake, there are lots of duplicates to this question. – Martin J. Mar 07 '14 at 04:20

3 Answers3

7

Yes, all those three are the same. And in them, that * is not dereferencing operator, it is part of the type int *, pointer to int.

Lee Duhem
  • 14,695
  • 3
  • 29
  • 47
  • Is that why some might want to put int* p_var instead of int *p_var? So that it is showing tied to the int? What's it called if it isn't the dereferencing operator? Thanks. – johnny Mar 07 '14 at 04:16
  • @johnny 1. I think so, but if you want to define more than one pointers, that style may be less clear, for example, `int* p1, *p2;` instead of `int *p1, *p2;`. 2. I do not know its name, if it has one. In C, it is intended that variable definition is looks like using of that same variable, for example, you define a pointer by `int *p;` and use it by `*p = ...`. – Lee Duhem Mar 07 '14 at 04:35
1

Yes, they are all identical. Whitespace is not significant in C. Which of the three you choose is entirely up to personal preference.

user229044
  • 232,980
  • 40
  • 330
  • 338
1

When you declare a variable, like you did in your example, the * symbol only tells to compiler that this variable is a pointer to the type you've selected.

Example:

int *p;

We have a declaration of a pointer of a integer. How we didn't initialized the pointer, it's value is a random memory address.

Example 2:

int a = 4;
int *p;
p = &a;
*p = 1;

In this example, it's possible to see the assignment of the memory address of a variable to the pointer p. The symbol & gets the variable a memory address. On the last line, we access the memory address associated with the pointer. In other words, the variable a area.

This is dereference.

Alan CN
  • 1,467
  • 1
  • 13
  • 13