-1

Possible Duplicate:
C - initialization of pointers, asterisk position

What is the difference between these declarations:

char* str; 

and

char *str; 

Is there a difference at all?

Another example:

   char* str; 
   struct StrStackLink *next; 

Are both str and next pointers or is there any significance in the placement of the star?

Community
  • 1
  • 1
Tom
  • 9,275
  • 25
  • 89
  • 147
  • Also similiar, http://stackoverflow.com/questions/2704167/type-declaration-pointer-asterisk-position, http://stackoverflow.com/questions/2660633/declaring-pointers-asterisk-on-the-left-or-right-of-the-space-between-the-type, http://stackoverflow.com/questions/2704167/type-declaration-pointer-asterisk-position – Krishnabhadra Dec 20 '12 at 09:22
  • downvoted: do some research. – Eregrith Dec 20 '12 at 09:39

7 Answers7

3

There is no difference - both declare a pointer to a char. There is a difference when you declare multiple variables on the same line however

char* str1, str2;

declares str1 to be a pointer and str2 to be a char, while

char *str1, *str2;

declares two char pointers

simonc
  • 41,632
  • 12
  • 85
  • 103
2

All of the char pointer declaration you wrote are equivalent. The placement of the star does change anything.

A case where it can be confusing is the following declaration

char* a, b;

Here, you declare a, a pointer to char, and b, a char. So I would recommand to collapse the star with the variable name, for clarity sake.

char *a, b;
tomahh
  • 13,441
  • 3
  • 49
  • 70
1

Nope, no difference at all. Just a matter of personal preference.

tom
  • 18,953
  • 4
  • 35
  • 35
1

There isn't any difference between the two.

SidR
  • 2,964
  • 1
  • 18
  • 32
1

No, there is no difference, but notation char *str is better, because you know that str is a pointer.

For example a declaration char *str1,str2 means that str1 is a pointer, but str2 is a char. A declaration char* str1,str2 means the same thing, but it's confussing.

banuj
  • 3,080
  • 28
  • 34
0

There is no difference.

Do you have any evidence that make you think that way?

You can also declare it this way:

char * p;
Zaka Elab
  • 576
  • 5
  • 14
0

There is not difference at all

both char *str and char* str are same, even

char * str; is also the same.

space doesn't effect this and * always goes with the identifier name.

Yes for 2nd question both next and str are pointers.

In the case below,

char *str,s; here str is pointer while s is only char. If you want to make both as pointer put *before each identifier.

Or, here is the use of typedef which you should consider in this case when you need more than one pointers of same type.

typedef char* pchar;

pchar str, s; // both str and s are pointers
Omkant
  • 9,018
  • 8
  • 39
  • 59