-1

Consider the following ANSI C code:

int* var1, var2; //line 1
int *var3, var4; //line 2
int * var5, var6; //line 3
int var7, *var8; //line 4

Are lines 1, 2 and 3 functionally equivalent? Does the spacing associated with '*' matter syntactically? Are 'var2', 'var4' and 'var6' also pointers here? Is line 4 legal?

I come from a Java background, so I've grown too comfortable with its verbosity and object-orientedness. Consequently, it's difficult now to gel with the primitivities of C.

phys
  • 1
  • 1
  • 1
    Spacing does not matter, but the `*` goes with the variable identifier, not the type. [Any good C book will cover these basics.](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) – ad absurdum Nov 25 '17 at 20:22

2 Answers2

0

Yes, line 1 to 3 are equivalent.

And no, var2, var4 and var6 are not pointers. Which is why in C the second line is usually preferred since it show the association of the asterisk to the variable (var3 in your case) a little better.

And lastly, yes line 4 is valid and declared var7 as a plain int variable, and var8 as a pointer to an int.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

Spacing between * and variable name doesn't matter in C. In front of which variable * is positioned that variable will be pointer type.

int* var1, var2; 

Here var1 type will be of pointer type & var2 is normal int variable.

int *var3, var4; 

Here var3 type will be of pointer type & var4 is normal int variable.

int * var5, var6; 

Here var5 type will be of pointer type & var6 is normal int variable.

int var7, *var8; 

Here var7 type will be of normal int type & var8 is pointer variable.

Achal
  • 11,821
  • 2
  • 15
  • 37