Dunc
Dunc

Reputation: 8038

C strings and pointers

When declaring a string in C what is the difference between

char* mystring;

and

char *mystring;

Upvotes: 1

Views: 136

Answers (2)

Jakub M.
Jakub M.

Reputation: 33817

No difference here. But those two below are different:

char *p1, *p2;

and

char* p1, p2;

Upvotes: 1

Jon
Jon

Reputation: 437336

There is no difference. The second option is commonly preferred because it makes it easier to avoid this pitfall:

char* str1, str2;

Here, str1 is a char* but str2 is a plain char. The other way of writing the declaration makes it easier to see that you have to put an extra asterisk in there:

char *str1, *str2;

Now both variables are of type char*.

Upvotes: 7

Related Questions