what does pointer to pointer do? char**
char x[16];
int y = 42 + 256;
char* p = &y;
int* q = p;
char** r = &p;
printf("%d %d %d\n", p, q, r);
why is the value of r always 12 units small开发者_运维问答er than p and q? Thank you!
What you're printing there are three memory addresses. Since those are allocated on the stack (assuming they're not global) I would expect something like:
2030 2026 2022
assuming a 32 bit machine and 4 byte pointers.
A double *
is a pointer to a pointer. If you use correct types it becomes clearer.
char *str = "hello world";
str
in this case is a pointer to a null-terminated sequence of characters.
char **p = &str;
means a pointer to a pointer to a string of characters.
I say "correct types" because you have, for example:
int y = 42 + 256;
char* p = &y;
Really this should be:
int *p = &y;
The usual reason to use a char *
in this instance is to examine the individual bytes.
Let's analyze your code line by line:
char x[16];
x
is an "array [16] of char
", i.e., it can store 16 char
values. You don't use x
at all in your code, by the way.
int y = 42 + 256;
y
is an int
equal to 298.
char* p = &y;
p
is a pointer to char
, and you're trying to assign the address of an int
to it. If you want a pointer to an int
, you should declare p
as int *p
, or if you want a generic pointer, you should declare it as void *p
. If you really want to do what you're doing, you need a cast: char *p = (char *)&y;
. The result of your initialization of p
is that p
points to the lowest addressed byte of y
.
int* q = p;
q
is of type int *
, and p
is of type char *
. Again, you need a cast. Assuming the cast, the standard says this about your last two statements:
A pointer to an object or incomplete type may be converted to a pointer to a different object or incomplete type. If the resulting pointer is not correctly aligned for the pointed-to type, the behavior is undefined. Otherwise, when converted back again, the result shall compare equal to the original pointer.
So, the net effect is as if you did: int *q = &y;
. So, q
contains the address of y
.
char **r = &p;
r
is of type char **
. &p
is of type int **
. Again, you need a cast at the least, but you can't portably convert an int **
to a char **
. I am not sure why you're using char **
anyway. If you wanted the address of p
in a variable, you should use void *
, or assuming you really want char
, use char *
.
printf("%d %d %d\n", p, q, r);
You're printing pointers with %d
format specifier. %d
takes an int
, not a pointer. You should use %p
and cast p
, q
, and r
to void *
for printing.
Assuming I understood what you wanted to do, I rewrote your program:
#include <stdio.h>
int main(void)
{
int y = 42 + 256;
void *p = &y;
void *q = p;
void *r = &p;
printf("%p %p %p\n", p, q, r);
return 0;
}
On my computer, this prints:
0x7fff5fbff7dc 0x7fff5fbff7dc 0x7fff5fbff7d0
p
and q
are obviously equal, and r
is the address of the object p
.
If I didn't understand your intent correctly, you have to tell us what you're trying to do and why.
Double pointer is a pointer to pointer.
why is the value of r always 12 units smaller than p and q?
That won't stay if you move to another runtime or another operating system / compiler.
In your code, there's no guarantee about the value of r compared to p or q. Since you say it's smaller, I'd guess these are automatic variables in a function. What you're seeing is the fact that the compiler is (probably) allocating 4 bytes for each pointer, and you've allocated 3 of them, which works out to 12 bytes -- that's pretty much an accident though, not anything the C standard requires or anything like that. Just for example, on a 64-bit system, it's pretty likely you'd see different numbers.
A char ** is a pointer to a pointer to a char. A typical use would be creating a dynamic array of strings. It might be easier to follow if you started with something like:
typedef char *string;
string *string_array;
If you put those pieces together, it says: char **string_array;
精彩评论