What happens when I declare say multiple variables on a single line? e.g.
int x, y, z;
All are ints. The question is what are y and z in the following statement?
int* x, y, z;
Are they all int pointers?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Only
xis a pointer to int;yandzare regular ints.This is one aspect of C declaration syntax that trips some people up. C uses the concept of a declarator, which introduces the name of the thing being declared along with additional type information not provided by the type specifier. In the declaration
the declarators are
*x,y, andz(it’s an accident of C syntax that you can write eitherint* xorint *x, and this question is one of several reasons why I recommend using the second style). The int-ness ofx,y, andzis specified by the type specifierint, while the pointer-ness ofxis specified by the declarator*x(IOW, the expression*xhas typeint).If you want all three objects to be pointers, you have two choices. You can either declare them as pointers explicitly:
or you can create a typedef for an int pointer:
Just remember that when declaring a pointer, the
*is part of the variable name, not the type.