Home > Net >  Pointer seems not be sync with printf's output
Pointer seems not be sync with printf's output

Time:04-09

I have trouble understanding what pointer2 contains. The second printf prints llo World, but the third one prints Hey you guys!. Why would it be like that if strcpy copies y you guys!\n into llo World. From my understanding of the below program the last output supposed to be llo Worldy you guys!\n, isn't it?

int main() 
{
    char str_a[20];  // a 20 element character array
    char *pointer;   // a pointer, meant for a character array
    char *pointer2;  // and yet another one

    strcpy(str_a, "Hello World\n");

    pointer = str_a; // set the first pointer to the start of the array
    printf("%p\n", pointer);

    pointer2 = pointer   2; // set the second one 2 bytes further in
    printf("%s", pointer2);       // print it

    strcpy(pointer2, "y you guys!\n"); // copy into that spot
    printf("%s", pointer);        // print again
}

CodePudding user response:

The pointer pointer points to the first character of the array str_a.

pointer = str_a;

The array contains the string "Hello World\n".

The pointer pointer2 points to the third element of the string

pointer2 = pointer   2;

that is it points to "llo World\n".

Then this substring is overwritten keeping unchanged str_a[0] and str_a[1].

strcpy(pointer2, "y you guys!\n");

So the array str_a contains the string "Hey you guys!\n"

In fact the above call of strcpy is equivalent to

strcpy( &str_a[2], "y you guys!\n");

because in turn this statement

pointer2 = pointer   2;

is equivalent to

pointer2 = &str_a[2];

or

pointer2 = &pointer[2];

And this call

printf("%s", pointer); 

outputs the string.

That is "He" (starting from str_a[0]) plus "y you guys!\n" (starting from str_a[2])yields the result string.

CodePudding user response:

char str_a[20];  // a 20 element character array
char *pointer;   // a pointer, meant for a character array
char *pointer2;  // and yet another one

The first line creates and allocates memory to 20 characters. The other two only create pointers to nothing. These pointers can be used to point to a memory region, what means that you can store an address (number) inside them.

strcpy(str_a, "Hello World\n");

This line copy "Hello World\n" to str_a (an allocated memory - OK).

pointer = str_a; // set the first pointer to the start of the array
printf("%p\n", pointer);

Now, we copy the address of str_a to pointer variable. These two variables can be used the same way. They point to the same memory. The memory address pointed is printed.

pointer2 = pointer   2; // set the second one 2 bytes further in
printf("%s", pointer2);       // print it

Here we copy an address (a number) too, like it was done before, but we add 2 to de address. So, if str_a and pointer point to a position X, now, pointer2 will point to X 2 (X is a number, the memory address of the block). We know that this block (str_a) has the content "Hello World\n", and then, pointer2 points to a position 2 chars to right: "llo World\n". This only means the address number stored by pointer2 points to this position, but the allocated block contains the whole sentence yet.

strcpy(pointer2, "y you guys!\n"); // copy into that spot
printf("%s", pointer);        // print again

Now, we can see a copy of characters to the address pointed by pointer2. So, the first two characters are outside of the copy location, and "y you guys!\n" will be copied to position 2 of str_a, that is position 0 of pointer2.

The result: "He" (two first characters of str_a untouched) "y you guys!\n" (the copied characters to pointer2) = "Hey you guys!\n"

If you print pointer2, you will see "y you guys!\n".

  • Related