What is a `char*`?

It is a pointer to a char.

When declaring a pointer, the asterisk goes after the type and before the identifier, with whitespace being insignificant. These all declare char pointers:

char *pointer1;
char* pointer2;
char * pointer3;
char*pointer4;    // This is illegible, but legal!

To make things even more confusing, when declaring multiple variables at once, the asterisk only applies to a single identifier (on its right). E.g.:

char* foo, bar;    // foo is a pointer to a char, but bar is just a char

It is primarily for this reason that the asterisk is conventionally placed immediately adjacent to the identifier and not the type, as it avoids this confusing declaration.

Leave a Comment