2

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
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    Firstly, welcome to the StackOverflow. The `pointer2` holds the address of the first `l` in `Hello World` string. After printing it once, the data it points to is being overwritten by the string `y you guys`! The string is written to the memory starting from the `pointer2` – Caglayan DOKME Apr 08 '22 at 16:39
  • 1
    Why would you expect, `strcpy` to append the string at the end? Besides that, your array can only hold 20 characters. You expect it to hold more than that. – Gerhardh Apr 08 '22 at 16:43
  • @CaglayanDOKME How does `pointer` gets modified though? `strcpy` doesn't mess with it. – userfaultfd Apr 08 '22 at 16:44
  • 1
    `pointer` is not modified. It points to `str_a`, similar as `pointer2` points into the array. That array is modified. – Gerhardh Apr 08 '22 at 16:46
  • @Gerhardh I think I have misconception about what `pointer + 2` actually does.. What part of the string does it take, the `He` or the rest? – userfaultfd Apr 08 '22 at 16:49
  • nvm boys. thx you – userfaultfd Apr 08 '22 at 17:00

2 Answers2

1

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Where does the `He` waits? Why is `He` being added and not the rest of the string that was cut by `pointer2`? – userfaultfd Apr 08 '22 at 16:47
  • @userfaultfd The elements of the array str_a[0] and str_a[1] were not overwritten. The new string was written starting from str_a[2]. And the pointer pointer outputs the result array starting from str_a[0]. – Vlad from Moscow Apr 08 '22 at 16:50
  • Yep, I understand now. My problem was that I thought that a pointer is an object by itself. I didn't know that by `strcpy`ing a pointer the place it is pointing is also changed. Gotta go back to pointers ig. – userfaultfd Apr 08 '22 at 16:56
  • @userfaultfd: A pointer is an object by itself, one that points at another object, `strcpy` changes the object pointed at by the pointer, not the pointer (it remains unchanged) – Chris Dodd Apr 08 '22 at 18:10
0
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".