PDA

View Full Version : Coding style about pointer



vql
4th February 2007, 17:06
I read a lot ebooks about C++. There are 2 way to declare a pointer, I don't know we should use which way although they are similar? And give me a reason.


char* str;
or
char *str;

Thanks.

jacek
4th February 2007, 17:17
There's also a third way:
char * str;An it really doesn't matter which one you choose, as long as you use it consistently.

sunil.thaha
5th February 2007, 06:49
I second jacek on this. This is a very small thing and consistency is what matters. I just want to point out that Qt used the second notation that is char *str;

Rationale:


char* str1, str2; // here str1 is a char* and str2 is a char;

char *str1, str2; // Here the declaration is more clear since the * is attached with the variable


and FYI Qt uses the char *str; notation

wysota
5th February 2007, 09:03
It really doesn't matter :) The parser regognizes non-alphanumeric characters as token boundaries, so you may put as much spaces or other characters there. These all are valid as well:

char
*
str1,
str2
;


char *str1
,str2;


x=5;

x = 5;

x
=5;

It is all just about source code readability.

sunil.thaha
5th February 2007, 09:07
It is all just about source code readability. Exactly !!

The point is that char *str1, str2 seems to be more clear ( to me ). That is it. And I found this practice in Qt Sources.