Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Address of array - difference between having an ampersand and no ampersand

Tags:

c

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?

like image 355
Alec Avatar asked Oct 09 '11 23:10

Alec


People also ask

Why is ampersand not used in array?

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.

What is difference between arr and &arr?

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.

What's difference between array and &array for int array 5?

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).

Why address of array is the same?

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.


2 Answers

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);
like image 90
John Bode Avatar answered Oct 08 '22 05:10

John Bode


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].

like image 38
Mahesh Avatar answered Oct 08 '22 07:10

Mahesh