Take the 2-minute tour ×

So I've been watching lectures from a University about C++, and I'm learning a lot, but one thing I still cannot understand is this:

Why do you have to do this sometimes?

char* test = "testing";

From what I've read/watched, I just don't understand why you have to put the *. From what I thought I understood, you only use * if you have an address, but maybe I'm just dead wrong.

share|improve this question
This isn't strictly correct, you should say const char * test = "testing";. String literals are pointers to constant chars. –  Kerrek SB Jul 25 '11 at 22:48
@Kerrek: Actually, string literals are constant char arrays which can decay into pointers to constant chars. –  fredoverflow Jul 25 '11 at 23:02
@Fred: Yes, even better! –  Kerrek SB Jul 25 '11 at 23:13

7 Answers 7

up vote 10 down vote accepted

This is a char:

char c = 't';

It can only hold one character at a time! :)

This is a C-string:

char sz[] = "test";

It can "hold" more than just one character.

A char* points to the memory location of a sequence of multiple chars.

char sz[] = {'t', 'e', 's', 't', 0};

const char *psz = "test";


*psz == 't';
*(psz + 1) == 'e';
*(psz + 2) == 's';
*(psz + 3) == 't';
*(psz + 4) == 0; // NUL


sz[0] == 't';
sz[1] == 'e';
sz[2] == 's';
sz[3] == 't';
sz[4] == 0; // NUL
share|improve this answer
Maybe use '\0' or 0 to terminate the strings. NULL is the null pointer constant, not the string terminator. –  Robᵩ Jul 25 '11 at 23:07
@Rob I didn't want to bring in backslashes into the picture, so I'll use 0. (Though I think NULL shows the intent more clearly.) –  Mateen Ulhaq Jul 25 '11 at 23:14
@muntoo: I'd use NUL, which is the ASCII-name for the zero-bits character. –  Jerry Coffin Jul 25 '11 at 23:16
@JerryC You mean like this? Isn't NUL just an abbreviation? –  Mateen Ulhaq Jul 25 '11 at 23:20
@muntoo: Looks better to me, yes. –  Jerry Coffin Jul 25 '11 at 23:21

The char type can only represent a single character. When you have a sequence of characters, they are piled next to each other in memory, and the location of the first character in that sequence is returned (assigned to test). Test is nothing more than a pointer to the memory location of the first character in "testing", saying that the type it points to is a char.

share|improve this answer

You can do one of two things:

char *test = "testing";


char test[] = "testing";

Or, a few variations on those themes like:

char const *test = "testing";

I mention this primarily because it's the one you usually really want.

The bottom line, however, is that char x; will only define a single character. If you want a string of characters, you have to define an array of char or a pointer to char (which you'll initialize with a string literal, as above, more often than not).

There are real differences between the first two options though. char *test=... defines a pointer named test, which is initialized to point to a string literal. The string literal itself is allocated statically (typically right along with the code for your program), and you're not supposed to (attempt to) modify it -- thus the preference for char const *.

The char test[] = .. allocates an array. If it's a global, it's pretty similar to the previous except that it does not allocate a separate space for the pointer to the string literal -- rather, test becomes the name attached to the string literal itself.

If you do this as a local variable, test will still refer directly to the string literal - but since it's a local variable, it allocates "auto" storage (typically on the stack), which gets initialized (usually from a normal, statically allocated string literal) on every entry to the block/scope where it's defined.

The latter versions (with an array of char) can act deceptively similar to a pointer, because the name of an array will decay to the address of the beginning of the array anytime you pass it to a function. There are differences though. You can modify the array, but modifying a string literal gives undefined behavior. Conversely, you can change the pointer to point at some other chars, so something like:

char *test = "testing";

if (whatever)
    test = "not testing any more";

...is perfectly fine, but trying to do the same with an array won't work (arrays aren't assignable).

share|improve this answer

Using a * says that this variable points to a location in memory. In this case, it is pointing to the location of the string "testing". With a char pointer, you are not limited to just single characters, because now you have more space available to you.

share|improve this answer

char* represents the address of the beginning of the contiguous block of memory of char's. You need it as you are not using a single char variable you are addressing a whole array of char's

When accessing this, functions will take the address of the first char and step through the memory. This is possible as arrays use contiguous memory (i.e. all of the memory is consecutive in memory).

Hope this clears things up! :)

share|improve this answer

The main thing people forgot to mention is that "testing" is an array of chars in memory, there's no such thing as primitive string type in c++. Therefore as with any other array, you can't reference it as if it is an element.

share|improve this answer

In C a array is represented by a pointer to the first element in it.

share|improve this answer
No, it isn't. Arrays are not pointers. –  fredoverflow Jul 25 '11 at 23:03
i don't said that they are pointers –  Mythli Jul 25 '11 at 23:29

Your Answer


