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
You have three memory blocks:
0x56xsxxxxxxxx
str +-----+-----+-----+-----+-----+-----+
0x7ffcxxxxxxx +-->| 'H' | 'e' | 'l' | 'l' | 'o' | 0 |
+-----------------+ | +-----+-----+-----+-----+-----+-----+
| 0x56xsxxxxxxxx -----+
+-----------------+
| 0x56xsxxxxxxxd -----+ 0x56xsxxxxxxxd
+-----------------+ | +-----+-----+-----+-----+-----+-----+
+-->| 'W' | 'o' | 'r' | 'l' | 'd' | 0 |
+-----+-----+-----+-----+-----+-----+
str[0]
evaluates to the first element of str
. Its value is 0x56xsxxxxxxxx
, the address of the "Hello array".&str[0]
evaluates to the address of the first element of str
. This is the same thing as the address of str
, which is 0x7ffcxxxxxxx
.str[0][0]
evaluates to the first element the "Hello array".&str[0][0]
evaluates to its address, 0x56xsxxxxxxxx
.str[1]
evaluates to the second element of str
. Its value is 0x56xsxxxxxxxd
, the address of the "World array".&str[1]
evaluates to the address of the second element of str
. This is the same thing as the address of str
plus the size of a pointer, which is 0x7ffcxxxxxxd
.str[1][0]
evaluates to the first element the "World array".&str[1][0]
evaluates to its address, 0x56xsxxxxxxxd
.