Question about pointers, a style issue...

Started by
7 comments, last by Springer 21 years, 7 months ago
When creating pointers which style do you prefer to use: int* pPointer (or) int *pPointer which style is more accepted or used? also when you create a pointer like.... char *pPointer = "I am the greatest hehe"; then in your code you have.. cout << *pPointer << endl; does it matter if you put the little star in the cout statement? If it will work either way, which way should i do it? With or without the little star in the cout statement? thanks EDIT::I just made a program to test and see if the * is needed in the cout statement ( it isnt) but i would still like to know which is the more preferred way; with or without the star? thanks again [edited by - Springer on October 5, 2002 9:07:56 PM]
Advertisement
I''m not going to answer your first question as I really don''t know, but I don''t think it matters much.

quote:
also when you create a pointer like....
char *pPointer = "I am the greatest hehe";
then in your code you have..
cout << *pPointer << endl;

does it matter if you put the little star in the cout statement?
If it will work either way, which way should i do it?
With or without the little star in the cout statement?


Putting in the star (asterisk) in this case would likely be an error. Try compiling as it is (the result: "I"). The reason this is true is that << is overloaded to pass a variety of variables into cout. When you place the * in front of pPointer, you dereference the pointer, or pass the information pointed to by pPointer (i.e. the single character "I"). However, when you omit the *, you pass in the actual pointer which is then assumed to be a string (char*) and it displays the entirety of the string.

Hope that helps a bit.
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
DOH! i didnt even catch that!

I just saw that it was compiling without error so i didnt pay much attention to the output

Now i really feel stupid.


btw, i would also like to get more feedback on my first question, just a matter of style that is all.
I''ve tried out both styles, and I found no inherent readability difference. Since the compiler treats the asterisk as belonging to the variable and not the type, I ended up aligning it to the right.

I think the important thing is to be comfortable reading either style.
In your example, the question really is about style. However, if you ever declare multiple variables of the same type in one line, then the choice is more apparent:

char *pOne = "Hi", *pTwo = "There";

The compiler only uses the ''*'' for the immediate variable name, not all in the line. So, in my example above, I have declared two pointer variables. If you write it this way:

char* pOne = "Hi", pTwo = "There";

Then the variable pTwo will be a char, not a char pointer. You could of course write:

char* pOne = "Hi",* pTwo = "There";

But I think it becomes less legible for reading purposes.

(Of course this leaves the next question, whether declaring more than one variable in the line is a good style...:-) Not to mention commenting, etc.
Wow, AP, that's a really important point that's sure to save me a serious headache in the future.

[edited by - Thunder_Hawk on October 5, 2002 10:43:30 PM]
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
And yet there is one more problem to consider with the style options. Consider having a reference to a pointer. However would write it: TYPE *& or TYPE &*?

If you don''t already know, it is TYPE *&.

My point is that if you put the pointer on the type and the reference on the instance''s name, then you will never have a problem with declaring references to pointers. In other words:
int* &ref_to_pointer_to_int; 
I prefer to label pointers as int* pPointer because I see it as part of the type (no matter how the compiler sees it). I like to distinguish between a pointer and a dereference, rather than make them look so similar. Same with references, i.e. int& RefInt = *ReturnsPointerToInt(); etc. I prefer that it not look like the address-of operation, for which I put the '&' next to the variable.

It bugs me a little when I deal with code that does it the other way. I'm also against putting the open curly brace to the right of the statement. I consider both in the same code to be a sign of pure evil.

Value of good ideas: 10 cents per dozen.
Implementation of the good ideas: Priceless.
Proxima Rebellion - A 3D action sim with a hint of strategy

[edited by - BS-er on October 5, 2002 11:01:49 PM]
Value of good ideas: 10 cents per dozen.Implementation of the good ideas: Priceless.Machines, Anarchy and Destruction - A 3D action sim with a hint of strategy
quote:Original post by BS-er
I prefer to label pointers as int* pPointer because I see it as part of the type (no matter how the compiler sees it). I like to distinguish between a pointer and a dereference, rather than make them look so similar. Same with references, i.e. int& RefInt = ReturnsInt(); etc. I prefer that it not look like the address-of operation, for which I put the ''&'' next to the variable.
I like these reasons . It might be time for me to switch over...
quote:Original post by BS-er
It bugs me a little when I deal with code that does it the other way. I''m also against putting the open curly brace to the right of the statement. I consider both in the same code to be a sign of pure evil.
Even though you might not use the style, you should try not to hate it. If you can learn to tolerate it, you''ll be a lot happier

This topic is closed to new replies.

Advertisement