GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Dec-2004, 20:21
BobbyMurcerFan BobbyMurcerFan is offline
Member
 
Join Date: May 2004
Posts: 103
BobbyMurcerFan is on a distinguished road

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:
int *piWeight;

// Or
int* piWeight;

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:
// Prototype
int MyFunc(char* MyString);

//Pointer to MyFunction
int (*pMyFunc) (char* MyString);    // will work

int* (pMyFunc) (char* MyString);    // won't work b/c of type mismatch

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  
Old 06-Dec-2004, 20:30
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,534
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice
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  
Old 06-Dec-2004, 20:31
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,621
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by BobbyMurcerFan
I've seen pointers declared in two different manners:

type *identifier
type* identifier

For example:
CPP / C++ / C Code:
int *piWeight;

// Or
int* piWeight;

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:
// Prototype
int MyFunc(char* MyString);

//Pointer to MyFunction
int (*pMyFunc) (char* MyString);    // will work

int* (pMyFunc) (char* MyString);    // won't work b/c of type mismatch

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.


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:
int* a, b;
This kind of looks like a and b are pointers to int, but actually a is a pointer and b is an int.

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:
int *a, *b;
This is more natural to me since i usually use "int *a;" rather than "int* a;" anyhow. If you think of a good reason to do it some other way, no hard feelings; I have no emotional attachment to either way of expressing it.

It works for me, but Your Mileage May Vary.

Regards,

Dave
  #4  
Old 07-Dec-2004, 07:41
BobbyMurcerFan BobbyMurcerFan is offline
Member
 
Join Date: May 2004
Posts: 103
BobbyMurcerFan is on a distinguished road
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 GIDBlogLast Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 22:49.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.