when i declare
why the type of ptr_b is intCode:int* ptr_a, ptr_b;
and not int*
???
when i declare
why the type of ptr_b is intCode:int* ptr_a, ptr_b;
and not int*
???
If you spaced it out like this it may become clearer...
(there is no asterisk on the ptr_b)Code:int *ptr_a, ptr_b;
Perhaps you wanted:
Code:int *ptr_a, *ptr_b;
so its illegal to write??
my goal was to declare 2 variables as pointer in one lineCode:int* ptr_a, ptr_b;
No it's not illegal. It just won't do what you wanted. You need to put the asterisk with each variable you wish to be a pointer.
whats the result of
the astrix near the intCode:int* ptr_a, ptr_b;
what will be the result of such line?
But if you don't want to place the * before the name, it's better to put each pointer on a separate row. Consistency is the argued most important thing. So pick the one you feel most comfortable with and stick with it.
ptr_a will be of type int*, ptr_b will be of type int.
The asterisk near the int is not the issue.
You could write
orCode:int* ptr
orCode:int * ptr
The fact is, it precedes the variable name. Somewhere. With or without misdirecting spaces. If you want a second pointer, it too needs to be preceded by an asterisk. You'd never realize that using Elysia's misinterpretation of pointer declaration.Code:int *ptr
Last edited by nonoob; 10-13-2008 at 01:46 PM.
Although it was written with C++ in mind, I suggest reading Stroustrup's answer to the FAQ Is ``int* p;'' right or is ``int *p;'' right?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Thank you, laserlight... It helped me understand where int* p comes from. I'm not a C++ person. I was about to post the following in response to Elysia:
The pitfall, if any, appears to be that you've settled on a syntactical foible that implies some associativity of basic C data type with the pointer indicator. This causes you to make strange recommendations about separate lines.
But now I realize there is a reason. But I took a long time to compose the above so I thought I'd voice it anyhow for posterity.
Nevertheless, this is not associated with C++. This style is equally valid in C and any good book should line out both alternatives, leaving the programmer to choose their preferred style.
But it's true that I heavily prefer emphasis on pointers binding to the types due to my C++ background.
Nevertheless it's a good advice, really, since defining pointers on separate lines avoids confusion and people thinking that you've made a (probable) error (as Bjarne clearly lines out in his faq).
And actually, I was looking for that article when replying earlier, but couldn't actually find it due to only bjarne's last name was in the title, so Firefox wouldn't find it. I bookmarked it now, so no problems now!
thanks