![]() |
|
#1
|
|||
|
|||
type* identifier or type *identifier for pointers?I've seen pointers declared in two different manners:
type *identifier type* identifier For example: CPP / C++ / C Code:
Now I realize C/C++ ignores whitespace so these declarations will be identical to the compiler. But is there any good reason to use one style over the other? I've noticed that most C/C++ books use the first method, but Stroustrup uses the second. However, when declaring pointers to functions, you must put the star next to the pointer name and not next to the type. For example: CPP / C++ / C Code:
Which do you prefer? Putting the star next to the type makes the most intuitive sense for me. But I'm still new to this, so if there is a good reason to do it the other way, I can adapt. Thank a lot. & sorry for asking such basic questions, but I'm trying to find a style to stick to. Thanks again. |
|
#2
|
||||
|
||||
|
As you already know, it's a matter of preference. My personal choice (after finding ways not to use pointers) is to keep the * w/ the variable: int *var. To me this makes more sense; you can see that var is a pointer to an int. If you have different variables (and choose to use the same word), you can easily see which is a pointer and which isn't. Plus, you don't forget to change when you use function pointers.
__________________
Common Sense v2.0-Striving to make the world a little bit smarter. |
|
#3
|
|||
|
|||
|
Quote:
I'm not one to disagree with Stroustroup, but I like door #1 better. Just habit, mostly. Sometimes you can absent-mindedly lose sight of the real deal with things like CPP / C++ / C Code:
Actually I usually put such declarations on separate lines, but if I did want two pointer declarations on the same line, I would do this: CPP / C++ / C Code:
It works for me, but Your Mileage May Vary. Regards, Dave |
|
#4
|
|||
|
|||
|
Thanks guys
As newbie, *variable_name sometimes looks like the variable is being dereferenced, that's why I prefer type*. But it can all change, LOL. (And I see your point Dave about when more than one variable is declared in the same statement.) Thanks again everyone. |
Recent GIDBlog
Last Week of IA Training by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Error C2146: syntax error : missing ',' before identifier 'C4' | mattchew008 | CPP / C++ Forum | 2 | 19-Dec-2004 06:06 |
| Help! undeclared identifier error | wfillis | .NET Forum | 8 | 25-Aug-2004 06:03 |
| Pointers-general confusion of pointer type. | warny_maelstrom | C Programming Language | 3 | 11-Mar-2004 21:26 |
| How Do i get php to find out the file type of a file for me? | viperman95833 | MySQL / PHP Forum | 2 | 08-Mar-2003 09:48 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The