I recently started learning C++ and came across with the concept of a pointer (which is a variable that stores the address of another variable). However I also came across with char* str = "Hello"
and I became confused. So it looks like the of "Hello" is being assigned to the pointer str (which I thought could only store addresses). So can a pointer also store a string?

- 3,541
- 28
- 38
- 38

- 43
- 4
-
1The pointer stores the address of the first character in the string, not the string itself. – Code-Apprentice May 24 '18 at 15:16
-
What book are you using to learn C++ that didn't cover this? – Fred Larson May 24 '18 at 15:16
-
It'll make sense when you reach arrays (and array decay to pointer). Also, a pointer is a variable which _points to_ another variable. If you stored the value, you'd just have a copy of the original. – Useless May 24 '18 at 15:20
-
You might want to go over your question and make sure you have pointer or value where you need it, and the right one each time. – Deduplicator May 24 '18 at 15:23
-
I am not reading any book to learn C++ and I'm just searching for some tutorials online and seeing some videos. So I think I got it the string (sequence of characters) is stored in memory and then the pointer "points" to the address of the first character. Would you recommend me some book or online tutorial? – DCochicho May 24 '18 at 15:24
-
2I *really* recommend you also read some books. [Here's a list of good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). – Some programmer dude May 24 '18 at 15:36
5 Answers
The pointer will store the address of the start of the string, therefore the first character. In this case "Hello" is an immutable literal. (Check the difference: Immutable vs constant)
More correctly, a pointer cannot store a string as well as anything, a pointer can point to an address containing data of the pointer's type.
Since char*
is a pointer to char
, it points exactly to a char
.

- 4,755
- 1
- 15
- 29
For future reference you should only use the language tag of the language you're using. C and C++ are two very different languages, and in this case there is a difference.
First the common part: Literal strings like "Hello"
are stored by the compiler as arrays. In the case of "Hello"
it's an array of six char
elements, including the string null terminator.
Now for the part that's different: In C++ such string literal arrays are constant, they can not be modified. Therefore it's an error to have a non-const pointer to such an array. In C the string literal arrays are not constant, but they are still not modifiable, they are in essence read-only. But it's still allowed to have a non-const pointer to them.
And finally for your question: As with all arrays, using them make them decay into a pointer to their first element, and that is basically what happens here. You make your variable str
point to the first element in the string literal array.
A little simplified it can be seen like this (in C):
char anonymous_literal_array[] = "Hello";
...
char *str = &anonymous_literal_array[0]; // Make str point to first element in array

- 400,186
- 35
- 402
- 621
In this example, the pointer is the address of the first character in the string. This is inherited from C where a "string" is an array of characters terminated by a NULL character. In C and C++, arrays and pointers are closely related. When you do your own memory management, you often create an array with a pointer to the first element of the array. That is exactly what is going on here with the array holding the string literal "Hello"
.

- 81,660
- 23
- 145
- 268
in c/c++ strings are stored as array of characters. Literal string like "Hello" actually return start of temporary read only character array which hold this string.

- 1,663
- 1
- 8
- 20
A char* variable is a pointer to a single byte(char) in memory. The most common way of handling strings is called a c-style string where the char* is a pointer to the first character in the string and is followed by the rest of the characters in memory. The c-string will always end in a '\0' or null character to signify that you've reached the end of the string ( 'H', 'e', 'l', 'l', 'o', '\0' ).
The "Hello" is called a string literal. What happens in memory is at the very beginning of your program, before anything else is run, the program allocates and sets the memory for the "Hello" string where the other static constants are located. When you write char* str = "Hello"; The compiler knows you're using a string literal and sets str to the location of the first character of that string literal.
But be careful though. All string literals are stored in a portion of memory that you cannot write to. If you try to modify that string, you might get memory errors. To make sure this doesn't happen, when dealing with c-strings, you should always write const char* str = "Hello"; That way the compiler will never allow you to modify that memory.
To have a modifiable string, you will need to allocate and manage the memory yourself. I would suggest using std::string, or have some fun and make your own string class that handles the memory.

- 32
- 6