Basic misunderstanding of C-style strings

To make sure my understanding of C-style strings was correct, I tried:

1
2
3
4
5
6
7
8
9
#include <cstdio>

int main()
{
    const char* greet = "W";
    std::printf("First, we have %i\n",*((bool*)(greet+1)));
    greet = "Hello world!";
    std::printf("Now, we have %i",*((bool*)(greet+1)));
}


So, if I'm right, a string enclosed in double quotes basically tells the compiler to allocate the memory for the array formed by those characters and a \0, and returns a pointer to the first char in the array. In which case setting greet to anything shouldn't affect any adjacent memory at all. But the result I got was:

First, we have 0
Now, we have 1

Which demonstrates that the bit directly after greet in the memory just got modified.

Where did I go wrong?
The expression (greet + 1) is moving the pointer one char to the right. In the first case, greet points to a read-only section of memory that contains the letter W and a null char. The expression (greet + 1) returns a pointer to the null char.

In the second case, you redirect the greet pointer to an entirely different section of read-only memory that contains the string Hello world! plus a null char. In this case (greet + 1) returns a pointer to the second char of the string and this time it is not a null char; this time it is letter e. A null char with a value of zero is interpreted as false (or zero), while any other value is interpreted as true, and this is why you get what you get.

It is not that the memory is modified, it is just that the pointer is told to point at a different section in RAM.
In the second case, greet points to the H, so greet+1 is the memory location holding the e.

Which demonstrates that the bit directly after greet in the memory just got modified.
No, it does not. It demonstrates that first greet pointed to the memory location holding the W, and then it was changed to point to the memory location holding the H. greet was changed. The contents of the memory holding the W and the Hello World! hasn't been changed.
Last edited on
i THINK it's because an array modifies places in memory, not just one place.

For example, when greet="W", here is the memory.

|blah|blah|W|blah|blah

but if you make greet ="Hello world!"

|blah|blah|H|e|l|l|o| |w|o

and so on.
Oh! Oops. For what I was trying to accomplish, I should've done &greet+1, not greet+1. And that works. Duh. Thanks!
Last edited on
So, if I'm right, a string enclosed in double quotes basically tells the compiler to allocate the memory for the array formed by those characters and a \0, and returns a pointer to the first char in the array.
Right so far.

In which case setting greet to anything shouldn't affect any adjacent memory at all.
Not quite right.

greet is a variable, a pointer to a constant char array in this case. That means, you can change the pointer, but not the thing pointed to. And that's exactly what you need. greet is initialised as you described above.

greet = "Hello world!"; reassigns greet from "W" to some other memory location that holds "Hello world!"

Remember, greet is a variable that can point to different thing (be assigned different values).
Last edited on
whovian wrote:
Oh! Oops. For what I was trying to accomplish, I should've done &greet+1, not greet+1.

What is it you are trying to do? I'm not seeing what the reference to the pointer will accomplish.

Edit: Also my output is
First, we have 0
Now, we have 101
and I dont quite understand why I'm getting 101 rather then 1.
Last edited on
Yea, basically what I'm trying to accomplish is seeing if setting greet equal to anything affects any adjacent memory.
In C++, setting a variable to some value will only cause a change in that variable.
Topic archived. No new replies allowed.