13

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)...?

Maxpm
  • 24,113
  • 33
  • 111
  • 170

4 Answers4

24

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.

Cameron
  • 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
16

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!

cadolphs
  • 9,014
  • 1
  • 24
  • 41
1

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.

suszterpatt
  • 8,187
  • 39
  • 60
1

Whitespace doesn't normally matter, so

char* suchandsuch;

char *suchandsuch;

char
*
suchandsuch;

are all the same.

Pizearke
  • 107
  • 7