0

Instinctively I found where to put spaces in pointer declarations:

int system(const char *command);   // <-- right
int* foo() { return 0; }           // <-- wrong
int *X = 123;                      // <-- right
int* Y = 321;                      // <-- wrong
int *Z = (int*) X += (int*)Y       // <-- right
#define pchar char*                // <-- right

but this still puzzles me:

typedef int* intptr;

OR

typedef int *intptr;

Where the star belongs, left or right?

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
exebook
  • 32,014
  • 33
  • 141
  • 226
  • It's a matter of preference. – Chad Feb 21 '14 at 05:53
  • In my opinion, the latter is correct. Keep in mind that '*' means 'contents of'. You are declaring a variable whose contents are a char. – Warren Dew Feb 21 '14 at 05:57
  • @user2580516 intptr isn't a variable, it's a type. It's an alias for the type `int*`. In languages with a better design, the type would be something like `^int`. And `*` does not mean "contents of" in a type, it means "pointer to [type]". – Jim Balter Feb 21 '14 at 06:01
  • @JimBalter If `*` means 'pointer to', how come `a = *b` results in `a` containing the contents of `b`, rather than a pointer to b? – Warren Dew Feb 21 '14 at 06:10
  • @user2580516 I said "in a type". If you don't understand the difference between a type declarator and an expression then you don't belong in this conversation. `int* x` means "x is a **pointer to** int". Oh, and `a = *b` doesn't get the contents of `b`, `a = b` does ... `*` is the *dereference* operator. These are C basics ... go learn them. I'm done here. – Jim Balter Feb 21 '14 at 18:55
  • @JimBalter if `b` is a pointer, then `a = b` just gets you another pointer. In c, a pointer or reference is designated by `&`, not `*`, speaking of c basics. – Warren Dew Feb 22 '14 at 00:15
  • @user2580516 `a = b` sets the contents of `a` to the contents of `b`. You're misusing the word contents -- the contents of a pointer variable is the pointer value, not what's stored there. And I've programmed in C for 40 years and was on the C Standards committee, so I know the basics. Enough now, this conversation doesn't belong here. – Jim Balter Feb 22 '14 at 09:27

1 Answers1

0

Both are equivalent. The choice is a matter or personal preference.

aldo
  • 2,927
  • 21
  • 36