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' : %pn", str[0]);
printf("adress of Hello strings : %pn", &str[0]);
printf("point to the string Hello and we point to the adress of 'H' (str[0][0]) : %cn", str[0][0]);
printf("adress of adress what str[0][0] points : %pn", &str[0][0]);
printf("n");
printf("adress of 'W' : %pn", str[1]);
printf("adress of World strings : %pn", &str[1]);
printf("point to the string World and we point to the adress of 'W' (str[1][0]) : %cn", str[1][0]);
printf("adress of what str[1][0] points : %pn", &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
mistipy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4
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 ofstr
. Its value is0x56xsxxxxxxxx
, the address of the “Hello array”.&str[0]
evaluates to the address of the first element ofstr
. This is the same thing as the address ofstr
, which is0x7ffcxxxxxxx
.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 ofstr
. Its value is0x56xsxxxxxxxd
, the address of the “World array”.&str[1]
evaluates to the address of the second element ofstr
. This is the same thing as the address ofstr
plus the size of a pointer, which is0x7ffcxxxxxxd
.str[1][0]
evaluates to the first element the “World array”.&str[1][0]
evaluates to its address,0x56xsxxxxxxxd
.
2
Your question why the adress of str[0]
is it different if I point into the same adress ? is confusing. Let me try an explanation:
Here is a modified version with more explicit output:
#include <stdio.h>
void dump_memory(const void *p, size_t len, int full) {
const unsigned char *pb = p;
int pos = printf("memory at %p:", p);
for (size_t i = 0; i < len; i++)
printf(" %02X ", pb[i]);
printf("n");
if (full) {
printf("%*s", pos, "");
for (size_t i = 0; i < len; i++) {
if (pb[i] == 0)
printf(" \0 ");
else
if (pb[i] >= ' ' && pb[i] < 0x7f)
printf(" %c ", pb[i]);
else
printf(" -- ");
}
printf("n");
}
}
int main(void)
{
const char *str[] = { "Hello", "World" };
printf("address of the `str` array: %pn", (void *)str);
printf("n");
printf("address of the first string pointer: %pn", (void *)&str[0]);
printf("first string pointer (`Hello`): %pn", (const void *)str[0]);
printf("address of the first character of the first string: %pn", (const void *)&str[0][0]);
printf("First character of the first string: %cn", str[0][0]);
printf("n");
printf("address of the second string pointer: %pn", (void *)&str[1]);
printf("second string pointer (`World`): %pn", (const void *)str[1]);
printf("address of the first character of the second string: %pn", (const void *)&str[1][0]);
printf("First character of the second string: %cn", str[1][0]);
printf("n");
dump_memory(str, sizeof(str), 0);
dump_memory(str[0], str[1] + 6 - str[0], 1);
return 0;
}
Output:
address of the `str` array: 0x16ce0f060
address of the first string pointer: 0x16ce0f060
first string pointer (`Hello`): 0x102ff3e12
address of the first character of the first string: 0x102ff3e12
First character of the first string: H
address of the second string pointer: 0x16ce0f068
second string pointer (`World`): 0x102ff3e18
address of the first character of the second string: 0x102ff3e18
First character of the second string: W
memory at 0x16ce0f060: 12 3E FF 02 01 00 00 00 18 3E FF 02 01 00 00 00
memory at 0x102ff3e12: 48 65 6C 6C 6F 00 57 6F 72 6C 64 00
H e l l o W o r l d
Explanation:
- The
str
array is located in the stack area of the running program, at address0x16ce0f060
for this particular run (it changes for every run on my macbook because the OS implements memory address randomisation as a countermeasure against hacking. - it spans 16 bytes: the space needed for 2
char *
on this 64-bit architecture. - the first 8 bytes contain
18 3E FF 02 01 00 00 00
, which is the representation in memory of the pointerstr[0]
, which is the address of the"Hello"
string (0x00000102ff3e12
as printed in hex). Notice that on my M2 CPU, the bytes in memory are stored from the least significant (12
) to the most significant (00
), which is known as little endian order. - the next 8 bytes contain
12 3E FF 02 01 00 00 00
, which is the representation in memory of the pointerstr[0]
, which is the address of the"World"
string (0x00000102ff3e18
as printed in hex). - the memory at address
0x102ff3e12
contains the actual strings, which happen to be stored consecutively. Each character is stored as a single byte in ASCII and a null terminator follows each string (00
or''
in C)
The actual addresses will differ on different systems, or a using different compiler and even for different runs of the same program. The relative position of the "Hello"
and "World"
strings may differ too, as well as the distance between them: a compiler could chose to align both strings, making "World"
start on an address that is a multiple of 8, and the strings could also be placed in different areas, causing this program to have undefined behavior as it dumps memory that may be inaccessible.
Pointers are simple objects that contain a memory address. Taking the address of what the pointer points to is a no-op: str[0]
and &str[0][0]
are the same thing, and so are &*str[0]
and &**str
(and among other weird alternatives &0[*str]
).
Memory layout (conceptual addresses):
str[] (array of pointers):
Address Content
0x2000 0x3000 -> Pointer to "Hello"
0x2008 0x3010 -> Pointer to "World"
"Hello" (null-terminated string literal):
Address Content
0x3000 'H' <- str[0][0], &str[0][0]
0x3001 'e'
0x3002 'l'
0x3003 'l'
0x3004 'o'
0x3005 ''
"World" (null-terminated string literal):
Address Content
0x3010 'W' <- str[1][0], &str[1][0]
0x3011 'o'
0x3012 'r'
0x3013 'l'
0x3014 'd'
0x3015 ''
Expression | Points to or Represents | Value/Address |
---|---|---|
str |
Address of the array of pointers | 0x2000 |
str[0] |
Address of the string literal "Hello" |
0x3000 |
&str[0] |
Address of the first pointer in the array of pointers str |
0x2000 |
&str[0][0] |
Address of the first character of "Hello" |
0x3000 |
str[1] |
Address of the string literal "World" |
0x3010 |
&str[1] |
Address of the second pointer in the array of pointers str |
0x2008 |
&str[1][0] |
Address of the first character of "World" |
0x3010 |