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.
Thanks.Code:
char* str; or char *str;
Printable View
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.
Thanks.Code:
char* str; or char *str;
There's also a third way:An it really doesn't matter which one you choose, as long as you use it consistently.Code:
char * str;
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:
Code:
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
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:
Code:
char * str1, str2 ;
Code:
char *str1 ,str2;
Code:
x=5;
Code:
x = 5;
Code:
x =5;
It is all just about source code readability.
Exactly !!Quote:
Originally Posted by wysota
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.