Reputation: 63
Context
I was studing about pointer in C, for that I was testing some pointer's algorithm and in one of this tries I make a little change that is bugging until now.
This is the code:
char *s = "ABCDEF";
char **y = &s;
printf("%p\n", s);
printf("%p\n", &s);
printf("%p\n", y);
printf("%p\n", &s[0]);
The problem is that for some reason when I ask for C to print the pointer of S (without the ampersand), I receive one pointer, but when I put the ampersand I receive another pointer differently. I think that this didn't happen because the ampersand will give me the address of S.
So to check if isn't a bug, I make a pointer with the address of S, and I received the same that the "&s" as expected, but when I use the "&s[0]" I received the value of the single "s".
So, why is C giving me two results if in both cases I'm just asking for the first character of the string?
The values that I received
0x400634
ox7ffd95c33090
ox7ffd95c33090
0x400634
I know that probably I'm forgetting some piece of logic, but I've struggled with this, so if someone can help me, I'll appreciate it.
Upvotes: 5
Views: 125
Reputation:
the pointer of S (without the ampersand)
This is already confusing. char *s
declares s
as a pointer.
but when I put the ampersand I receive another pointer
That other pointer can be called pointer of (or to) s
. A pointer to a pointer (to char).
address of S
"pointer of" and "address of" must mean the same. Nobody says "pointer of s" in that context above.
Upvotes: 0
Reputation: 212248
char *s = "ABCDEF";
char **y = &s;
printf("%p\n", s); /* address of the 'A' in the string literal "ABCDED" */
printf("%p\n", &s); /* address of the local variable s */
printf("%p\n", y); /* contents of y, which is the address of the local variable s */
printf("%p\n", &s[0]); /* address of the 'A' in the string literal */
Maybe it helps to think of s[0]
as *s
, in which case you can rewrite &s[0]
as &*s
and see that the &
and the *
operators "cancel" (in a very non-technical sense) each other.
Upvotes: 1
Reputation: 31
s[0] is equivalent to (s + 0), means that the first block of that memory is dereferenced so if you write &s[0] this is equivalent to &((s + 0)) -> (s+0) That is an actual pointer to the array's first element and the double-pointer can refer to a pointer, pointers themselves have addresses because they are similar to variables that store a memory address.
Upvotes: 2
Reputation: 123468
The variable s
stores the address of the string literal "ABDCDEF"
; the variable y
stores the address of the variable s
.
char ** char * char
+---+ +---+ +---+
y: | | ----> s: | | ------> |'A'|
+---+ +---+ +---+
|'B'|
+---+
|'C'|
+---+
|'D'|
+---+
|'E'|
+---+
|'F'|
+---+
| 0 |
+---+
Upvotes: 7
Reputation: 409176
With
char *s = "ABCDEF";
char **y = &s;
you basically have this:
+---+ +---+ +----------+ | y | --> | s | --> | "ABCDEF" | +---+ +---+ +----------+
That is, y
points to s
, and s
points to (the first character of) the string "ABCDEF"
.
Printing y
will print where it is pointing, which is the location of the variable s
.
Upvotes: 4