uhh ... does this mean that
will create 2 pointers ?Code:int *x,y;
since the declared type is int * ?
uhh ... does this mean that
will create 2 pointers ?Code:int *x,y;
since the declared type is int *...
so x and y are 2 pointers ... true ?
I define each var on separate line, initializing it and when required - giving small commant
comment is optional, meaningful name and initialization are avoided in very rear circumstances.Code:int messageNumber = 0; /* will store the message Number to process */
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
I generally don't; and this is part of the reason a drifted toward my preference in #29.
I don't consider it a pitfall of the language. And my preference aside, your comment with the mention of the case...
... brings to mind the further confusion available with the use of a typedef:Code:int *x,y;
Note that in this case, BlaX, that both a and b would be pointers to int. FWIWCode:typedef int *ip; ip a,b;
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Yeah, but then again, I consider that wrong because you are basically "naming" the variable *var, and not var.
A pointer is not a special variable... it's just another variable that you can dereference.
Therefore this is wrong: type *var
And this is right: type* var
This is all IMHO, of course.
I clearly see it as a pitfall simply becauseI don't consider it a pitfall of the language. And my preference aside, your comment with the mention of the case...
type *a, *b works, yet type* a, b does not.
If I can define a pointer as
type* var
and
type *var
then I should be able to create pointers using both
type* a, b
and
type *a, *b.
Clearly this isn't a case, I see it as a pitfall and it also introduces confusion.
Why they did it like this I can only guess...
I consider your case to be programmer's block.
The type of a, b, and c are quite obvious to me.Code:type *a, b, **c;
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
>> Why they did it like this I can only guess...
Can I try?
Since pointers can be declared from any type, including the keyword void, it makes sense to me that things are the way they are. The asterisk is mostly syntactic sugar, which would have been necessary anyway in order to make a distinction between what's being declared. You could have introduced a more explicit keyword like pointer, but I don't see how the same problem wouldn't arise in that case. It's just as easy for new programmers to be confused and assume that only variables of a certain type can be declared on a line, or forget to add the keyword at the end.
Not to mention that use of another keyword would have reserved another variable name for language purposes. Expression through repression! Yay!Code:int p pointer, q; /** oops, q is an int! **/
You could make sure that you declare the right thing on the same line through using typedefs, like Dave demonstrated. Perhaps the people who made the language decided if they wanted that, they had it, and if they didn't, those people were satisfied. Win-win.
Not so much as confusion, but I do consider it a pitfall since my preferred way of defining pointers just doesn't work.
Anyway, while we're still at the subject of typedefs... I like to always explicitly have a * at variables that are pointers, so I prefer:
Instead ofCode:void (myptr)(); myptr* p;
Of the horrors if you need a pointer to pointer:Code:void (*mypstr)(); myptr p;
Good gracious! That looks confusing. I'd rather it be:Code:void (*mypstr)(); myptr* p;
Which is where another pitfall to me appears... sometimes you've no choice but to embed the darn * into the actual typedef... Ah, sigh.Code:void (myptr)(); myptr** p;
Things can never be 100% as you want them, huh?
That's a discussion on personal style though, and i admit i was using the asterisk fixed to the type pointer notation for a long time, but switched to the fixed to the variable after some bugs with multi variable declarations.
A typical example of ...cheap programming practices.Code:... goto johny_walker_red_label; johny_walker_blue_label: exit(-149$); johny_walker_red_label : exit( -22$);