Archived

This topic is now archived and is closed to further replies.

MadKiwi

Pointer asterisk placement

Recommended Posts

Hi, I did a search on this but couldn''t find any results. I just want to know the difference between the following 2 statements or are they the same? Substitute the char for any type. char* c; char *c;

Share this post


Link to post
Share on other sites
This has been flamed many times over, but ultimately
char *c  
is better because
 char* a,b,c,d  
is NOT the same as
char *a, *b, *c, *d  


[edited by - Kaezin on March 22, 2004 5:09:41 PM]

Share this post


Link to post
Share on other sites
I agree Kaezin,

The only time I use "char*" instead of "char *" is when its the data type of a function. Else, Any variables are always "char *" for me.
Example:

char* myFunction(char *a, char *b);


- [BDS]StackOverflow

Share this post


Link to post
Share on other sites
quote:
Original post by BlueDev
I agree Kaezin,

The only time I use "char*" instead of "char *" is when its the data type of a function. Else, Any variables are always "char *" for me.
Example:

char* myFunction(char *a, char *b);


- [BDS]StackOverflow


Thanks for your quick answers guys, this forum is awesome. Regarding using char* for a function: will char * not work for a function or is that just your preferred format?

Share this post


Link to post
Share on other sites
Hey MadKiwi,

Q. will char * not work for a function or is that just your preferred format?

A. Just a preferred format. It's easier when you have preferences so when you look at your code things formulate faster than trying to figure out what this is, where it is, how it is, and etc...

Glad you are enjoying the boards There are some skilled programmers here (including myself ) that wont mind helping you out when you need.

If you ever feel that your post is not recieveing any replies, no worries, either no one is interested in the topic or the question is out of some of our league. Either way, I'll try replying with some sort of answer even if it isn't the best.


- [BDS]StackOverflow

[edited by - BlueDev on March 22, 2004 6:12:23 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Kaezin
... ultimately
char *c   
is better because
 char* a,b,c,d   
is NOT the same as
char *a, *b, *c, *d   


However,
char *a;
char *b;
char *c;
char *d;
is better than
char *a, *b, *c, *d;   
so your argument is moot. Now, I''m not trying to incite a religious war. No!

Share this post


Link to post
Share on other sites
I always preferred char* x to char *x. Mostly it''s because I choose to define "pointer-ness" as an attribute of a type rather than a variable. That is, an int pointer is a different type than an int.

As for the char* a, b, c, d, well that has always annoyed me so I avoid that construction except for rare cases where I do fall back to char *a, *b, *c, *d.


Ultimately it''s a matter of preference. There is no functional or syntactic different between the two.

Share this post


Link to post
Share on other sites
quote:
Original post by Kaezin
 char* a,b,c,d   
is NOT the same as
char *a, *b, *c, *d   



People who code like that have larger style issues to worry about than where they put their asterisks.

Share this post


Link to post
Share on other sites
quote:
Original post by Promit
I always preferred char* x to char *x. Mostly it''s because I choose to define "pointer-ness" as an attribute of a type rather than a variable. That is, an int pointer is a different type than an int.




That makes sense, but then the counter-argument to that is arrays. Definitely you would consider "array-ness" to be part of the type of the variable, but the array notation goes after the variable name:

int array[5]

so you can''t always group all the "type" information in one place.

Share this post


Link to post
Share on other sites
pinacolada : right, which is why associativeness in this case is one of the points of criticism when talking about c/c++.

this gets especially confusing when prototyping with arrays. when prototyping a function you don''t have to supply names so this : void foo(char) would be a legal prototype and void foo(char s[]) would be, too. but what about void foo(char[]) ??? (it is legal...)

Share this post


Link to post
Share on other sites
quote:
Original post by pinacolada
That makes sense, but then the counter-argument to that is arrays. Definitely you would consider "array-ness" to be part of the type of the variable, but the array notation goes after the variable name:

int array[5]

so you can''t always group all the "type" information in one place.
I think they do int[5] array; in Java or C#? I don''t know where, but I have seen it, and sometimes I unconciously typed it like that too and the compiler would usually go wild and spam the output window with at least 10 error messages.

p.s: I wanted to answer your question regarding the array-ness, but after the second thought that it might turn this innocent thread to a neverending debate, I decided not to.

Share this post


Link to post
Share on other sites
quote:
Original post by alnite
quote:
Original post by pinacolada
That makes sense, but then the counter-argument to that is arrays. Definitely you would consider "array-ness" to be part of the type of the variable, but the array notation goes after the variable name:

int array[5]

so you can''t always group all the "type" information in one place.
I think they do int[5] array; in Java or C#? I don''t know where, but I have seen it, and sometimes I unconciously typed it like that too and the compiler would usually go wild and spam the output window with at least 10 error messages.

p.s: I wanted to answer your question regarding the array-ness, but after the second thought that it might turn this innocent thread to a neverending debate, I decided not to.


I do believe that array-ness ought to be part of the type, it is like that in Java, and I think it ought to work the same way in all C derivatives.

C does not always agree with my preferences. I choose to tolerate that.

Share this post


Link to post
Share on other sites
yeah, i recently got back to code some stuff in c++, and this is really annoying..

in D, it was so nice.

int[SIZE] intArray;

int[] dynamicIntArray;

char[] charArray; // a string

int[char[]] map; // map<char[],int> a.k.a. std::map<std::string,int>

etc..

and of course int* x; if you needed a ptr..

well well, you can't have everything.

int* x; makes much more sence for anyone who thinks in types, than int *x; does. in C days, int *X did made more sence, it was a design decision. the, for a lot of people, wrong one.




If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

davepermen.net

[edited by - davepermen on March 22, 2004 7:43:56 PM]

Share this post


Link to post
Share on other sites