What’s the difference between char and char* in C++?

The variables with the * are pointers.

A ‘normal’ variable, for example a char or an int, contains the value of that datatype itself – the variable can hold a character, or an integer.

A pointer is a special kind of variable; it doesn’t hold the value itself, it contains the address of a value in memory. For example, a char * doesn’t directly contain a character, but it contains the address of a character somewhere in the computer’s memory.

You can take the address of a ‘normal’ variable by using &:

char c = 'X';
char * ptr = &c;  // ptr contains the address of variable c in memory

And you get the value in memory by using * on the pointer:

char k = *ptr;  // get the value of the char that ptr is pointing to into k

See Pointer (computing) in Wikipedia.

Leave a Comment