I have a struct that looks like this:
struct packet {
int a;
char data[500];
};
typedef struct packet packet_t;
I'm a little confused why the following code outputs the same address for each printf:
void myfunction() {
packet_t packet;
printf("%p\n", packet.data); //e.g., outputs 0x7fff1c323c9c
printf("%p\n", &packet.data); //e.g., outputs 0x7fff1c323c9c
}
Does anyone have a good explanation for this?
In case of a string (character array), the variable itself points to the first element of the array in question. Thus, there is no need to use the '&' operator to pass the address.
arr is an integer pointer (int*) which points the first element of the array. &arr is an integer array pointer (int*)[5] which points the whole array. (all five elements.) &arr is a pointer to an entire array.
Basically, “array” is a “pointer to the first element of array” but “&array” is a “pointer to whole array of 5 int”. Since “array” is pointer to int, addition of 1 resulted in an address with increment of 4 (assuming int size in your machine is 4 bytes).
The pointer to the array has the same address value as a pointer to the first element of the array as an array object is just a contiguous sequence of its elements, but a pointer to an array has a different type to a pointer to an element of that array.
Under most circumstances, an expression that has type "N-element array of T
" will be converted to an expression of type "pointer to T
", and its value will be the address of the first element in the array. This is what happens in the first printf
call; the expression packet.data
, which has type char [500]
, is replaced with an expression of type char *
, and its value is the address of the first element, so you're effectively printing &packet.data[0]
.
One exception to this rule occurs when the array expression is an operand of the unary &
operator; the type of the expression &packet.data
is char (*)[500]
(pointer to 500-element array of char
).
The address of an array is the same as the address of the first element, so both calls to printf
display the same value; it's just that the types of the expressions are different. To be pedantic, both expressions should be cast to void *
in the printf
calls (the %p
conversion specifier expects a void *
argument):
printf("%p\n", (void *) packet.data);
printf("%p\n", (void *) &packet.data);
That's because array decays to a pointer pointing to first element in the sequence. So, packet.data
address location is same as &packet.data
or &packet.data[0]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With