Came across this example in the book im reading and it didn’t make sense at all to me, I’m probably missing something but it seems like youre assigning count with the values ’10’ and then the value ‘x’ which isnt even an int. Just wondering if this is a syntax that is valid.
The book says this:
The variables count and x are declared to be integer variables in the normal fashion. On the next line, the variable intPtr is declared to be of type “pointer to int.” Note that the two lines of declarations could have been combined into a single line:
int count = 10, x, *intPtr;
here’s the program its taken from:
#import <Foundation/Foundation.h>
int main (int argc, char *argv[ ])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int count = 10, x;
int *intPtr;
intPtr = &count;
x = *intPtr;
NSLog (@"count = %i, x = %i", count, x);
[pool drain];
return 0;
}
This is just a declaration. Declaration consists of the initial part (declaration specifier) that describes the “basic” part of the type, and a comma-separated sequence of declarators, each of which declares a separate name and, possibly, modifies the basic type. In C you can declare multiple names using the same declaration specifier
is equivalent to
Optionally, you can add an initializer to each declarator or to some fo them. So
is the same as
That’s all there’s to it.