hi all,
what is the difference between static int a and int static a?
thanks
hi all,
what is the difference between static int a and int static a?
thanks
Whilst style dictates that you should use "static int", it's (apparently) valid to type "int static" - at least "gcc -Wall" doesn't complain about it. And from looking at the code, it does what you expect. I guess it's the same as "const int" or "int const".
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
That's because the '*' is a different kind of entity. "static" and "int" are a storage class specifier and a type specifier, respectively. They bind to the declared entity. Whereas '*' is an actual part of the declared entity.
In fact, the fragments "const int*" and "int* const" are essentially meaningless because the name the star binds to has been left out. Think of it this way:
This is not saying "x is a pointer to int." What it's really saying is, "*x is an int." Which of course implies that x itself must be a pointer to int. The star cannot be separated from the name. This is why I prefer the practice of attaching the star directly to the variable name instead of leaving a space. The star is NOT a type modifier.Code:int *x;
It's like using "unsigned int" or "int unsigned" or "long unsigned int long", I suppose. Type specifiers (like int and unsigned) and storage class specifiers (like static and extern) can go in any order.
There's no difference between static int a and int static a, it's just a matter of style and convention.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
It's like the difference between these:
I always put the asterisk next to the variable name, as brewbuck does, because otherwise it's confusing when you declare multiple variables at once. (It's true that some people don't recommend this . . . .)Code:int *x; int* x;
This
makes y look like a pointer, except it's not. You could, of course, use a typedef to make things easier.Code:int* x, y;
But I'm getting off topic . . . .Code:typedef int *int_p; int_p x, y;
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Expanding on what dwks said already... Consider:
This is, conceptually, declaring two integers: *x and y. If *x is an int, then x must be pointer to int. What it is NOT doing is declaring both to be pointer to int. It is easier to understand WHY it is this way if you think of it in the way I've outlined here.Code:int *x, y;
Writing the star directly next to the type can trigger the misconception that the star is binding to the type specifier and therefore affecting each declarator in the declaration. But the reality is it binds to the DECLARATOR, not the type specifier, and so it only affects the declarator directly to its right.
Writing the star directly adjacent to the declarator makes it more clear that it does in fact bind rightward, not leftward.
Oh, I see. I thought you were saying something that referred to a statement with only one declarator.
Thanks.
My philosophy is that int* is a pointer-type variable pointing to int, so I always place them next to the type, (int* is a type, just like int!) and I never ever do int* n, n2 - when there's a pointer declaration, I always place it on a row of its own. Avoids confusion.
Irrelevant. I find int* to be a type, whatever anyone might say. We can argue all we want about it, but the question is if anyone is truly correct.
Your example won't work, of course, since the second will be a normal int, but even if the language says so, I will still say int* is a type. It's the best understanding of pointers from my point of view. It makes them understandable.
(Plus the fact that any pointer is 4 bytes on x86, that kinda also adds to my interpretation of pointers; doesn't matter if it's char* or UINT64*, it's still 4 bytes, which is also why I consider them a type of their own.)