What is a char*
, exactly? Is it a pointer? I thought pointers had the asterisk before the identifier, not the type (which isn't necessarily the same thing)...?

- 24,113
- 33
- 111
- 170
-
11C doesn't care about spaces, so `char *foo;` is the same as `char* foo;` – Paul Tomblin Nov 27 '10 at 20:17
-
6@PaulTomblin: C does care about spaces, just not in that particular case. `char apple;` and `charapple;` are quite different. – Mooing Duck Jan 17 '12 at 17:47
-
My answer on a similar question: [What is a char*?](https://stackoverflow.com/a/6823292/365102). – Mateen Ulhaq Jul 28 '21 at 05:53
-
Not sure if the questions are duplicates since the other question talks more about strings, i.e. `const char* s = "test";` – Mateen Ulhaq Jul 28 '21 at 05:56
4 Answers
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.

- 96,106
- 25
- 196
- 225
-
Why is it, then, that when declaring multiple pointers on one line you have to use an asterisk for each one? For example, `char * Pointer1, * Pointer2;`. – Maxpm Nov 27 '10 at 20:20
-
I still do not understand why they designed it like that... `char*` seems much more natural to me (as a type). – jwueller Nov 27 '10 at 20:53
-
1@elusive: Yeah, me too :-) I often use `char*` myself, and limit myself to one declaration per line for clarity. – Cameron Nov 27 '10 at 21:06
-
What is confusing me is how can `char*` be a string? A string is an `Array` of chars - So why can we write code like `const char* str = "Hello";` - `char*` is only pointing to a single char not a string of chars? – Luke T O'Brien Jan 25 '21 at 14:20
-
An array of contiguous characters has to start somewhere in memory. The `char*` is simply pointing to the first character of that string -- ('H' in this case) but after that char is another, and another, so the pointer can be interpreted as pointing to a (null-terminated) string. – Cameron Jan 26 '21 at 17:20
It is a pointer to a character. You can write either
char* bla;
or
char *bla;
It is the same.
Now, in C, a pointer to a char was used for strings: The first character of the string would be where the pointer points to, the next character in the address that comes next, etc. etc. until the Null-Terminal-Symbol \0
was reached.
BUT: There is no need to do this in C++ anymore. Use std::string (or similar classes) instead. The char* stuff has been named the single most frequent source for security bugs!

- 9,014
- 1
- 24
- 41
http://cplusplus.com/doc/tutorial/pointers/
The *
character shows up in two distinct places when dealing with pointers. First, the type "pointer to T" is denoted by T*
(appending *
to the type name). Second, when dereferencing a pointer, which is done by prepending *
to the name of the pointer variable that you want to dereference.

- 8,187
- 39
- 60
Whitespace doesn't normally matter, so
char* suchandsuch;
char *suchandsuch;
char
*
suchandsuch;
are all the same.

- 107
- 7