I have a few questions regarding the code below.
- If I have a pointer of some type, what does it mean to use array indexing with it? in this example, what does
ptr[3]
stand for (ptr is a pointer of some type)? The output of the program is supposed to be
to be or not to be (Hamlet)
but I am not sure why, my problem is with the line(&ptr2)[3] = str
, I don't understand how does this line changes the third element of theptr1
array.int main() { char str[] = "hmmmm..."; const char *const ptr1[] = {"to be", "or not to be", "that is the question"}; char *ptr2 = "that is the question"; (&ptr2)[3] = str; strcpy(str, "(Hamlet)"); for (int i = 0; i < sizeof(ptr1) / sizeof(*ptr1); ++i) { printf("%s ", ptr1[i]); } return 0; }
Using this visualizer, we can see that ptr1 will point to str, I just don't understand why that happens.
Help appreciated.