2

Okay I am getting lost in these pointers can somebody exactly tell me what is(are) the difference between char * x,y,z;, char* x,y,z; and char (*)x,y,z; ? If you can please provide resources to your answer or something.

Naveen
  • 105
  • 1
  • 9
  • 1
    possible duplicate of [In C, what is the correct syntax for declaring pointers?](http://stackoverflow.com/questions/3280729/in-c-what-is-the-correct-syntax-for-declaring-pointers) – Shimul Chowdhury Feb 26 '15 at 05:20
  • @ShimulChowdhury: What about the one in parentheses? – Robert Harvey Feb 26 '15 at 05:20
  • 2
    The one in parentheses doesnt compile. If you meant char (*x)(), it's a function pointer: http://stackoverflow.com/questions/840501/how-do-function-pointers-in-c-work – dfranca Feb 26 '15 at 05:26

4 Answers4

2

The first two mean the same thing. They declare x as a pointer to a char, and y and z as char variables. The third one will cause a syntax error and, as @danielfranca pointed out in the comments, will not compile.

allieluu
  • 78
  • 2
  • 5
2

These two records

char * x,y,z; 
char* x,y,z;

are identical and equivalent to

char *x;
char y;
char z;

Take into account that these declarations are equivalent

char*x;
char* x;
char * x;
char *x;

All them declare variable x as pointer to char.

This record

char (*)x,y,z;

is invalid and will not be compiled.

I think you mean the following

char (*x),y,z;

In this case declaration

char ( *x );

is equivalent to

char *x;

You may enclose in parentheses a declarator. So the above record you could write like

char ( *x ), ( y ), ( z );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Okay so I got it almost but is `char (*) x,y,z;` same as `char *x,y,z` just curious or this too won't compile ?! – Naveen Feb 26 '15 at 05:39
  • @Disorted Casanøva No, they are no the same. The declarators are x and *x. You may write *( x ) or ( *x ). – Vlad from Moscow Feb 26 '15 at 05:48
  • Okay that means that basically will not compile. – Naveen Feb 26 '15 at 05:51
  • @Disorted Casanøva In this record ( * )x there are to adjacent declarators (abstract declarator ( * ) and identifier x). You may not place declarators such a way without separating them with a comma. – Vlad from Moscow Feb 26 '15 at 05:54
2

If you are asking the question, it is probably because you were looking for a shorthand way of declaring x, y and z all to be pointers to characters.

In most cases, you should just be clear about it:

char *x, *y, *z;

In more complicated situations where you will use it often, you can use a typedef:

typedef char *cp_t;
cp_t x, y, z;
cncsnw
  • 31
  • 3
2

Declare variables in the same way as you're gonna use them.

char *x, y, z;

*x is a char, y is a char, z is a char. So x is a pointer to a char.

void f(void), (*g)(void);

f and *g are functions without parameters or return value. So g is a pointer to such a function. The parentheses around *g are needed because (void) binds more tightly than *.

potrzebie
  • 1,768
  • 1
  • 12
  • 25