cpointers

Am i missing something about pointers?


i'm actually in tech school and i studied about pointers but there are little misunderstood about that.

Here is what i don't understand :

alright, str[0] is the same things about &str[0][0] but why the adress of str[0] is it different if i point into the same adress ?

int main(void)
{
    char *str[] = {"Hello", "World"};

    printf("adress of 'H' : %p\n", str[0]);
    printf("adress of Hello strings : %p\n", &str[0]);
    printf("point to the string Hello and we point to the adress of 'H' (str[0][0]) : %c\n", str[0][0]);
    printf("adress of adress what str[0][0] points : %p\n", &str[0][0]);
    printf("\n");
    printf("adress of 'W' : %p\n", str[1]);
    printf("adress of World strings : %p\n", &str[1]);
    printf("point to the string World and we point to the adress of 'W' (str[1][0]) : %c\n", str[1][0]);
    printf("adress of what str[1][0] points : %p\n", &str[1][0]);
}

Here is the return output:

adress of 'H' : 0x56xsxxxxxxxx
adress of Hello strings : 0x7ffcxxxxxxx
point to the string Hello and we point to the adress of 'H' (str[0][0]) : H
adress of adress what str[0][0] points : 0x56xsxxxxxxxx

adress of 'W' : 0x56xsxxxxxxxd
adress of World strings : 0x7ffcxxxxxxd
point to the string World and we point to the adress of 'W' (str[1][0]) : W
adress of what str[1][0] points : 0x56xsxxxxxxxd

Solution

  • You have three memory blocks:

                              0x56xsxxxxxxxx
    str                       +-----+-----+-----+-----+-----+-----+
    0x7ffcxxxxxxx         +-->| 'H' | 'e' | 'l' | 'l' | 'o' |  0  |
    +-----------------+   |   +-----+-----+-----+-----+-----+-----+
    | 0x56xsxxxxxxxx -----+
    +-----------------+
    | 0x56xsxxxxxxxd -----+   0x56xsxxxxxxxd
    +-----------------+   |   +-----+-----+-----+-----+-----+-----+
                          +-->| 'W' | 'o' | 'r' | 'l' | 'd' |  0  |
                              +-----+-----+-----+-----+-----+-----+