简体   繁体   中英

Casting an array to a struct pointer

 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>

 #define MAX_PARMS 20
 #define DATA_MAX 50
   struct s {
        uint8_t cmd;
        uint8_t main;         
        uint8_t sub;           
        uint8_t index;  
        uint8_t reg;            
        uint8_t sendlen;    
        uint8_t reclen; 
        uint8_t parm[MAX_PARMS];
    };
    struct t {
        uint8_t hdr;    
        uint8_t data[DATA_MAX];
        uint8_t len;    
    };

int main()
{
    struct t *p = malloc(sizeof(struct t));
    p->data[0] = 0xBC; 
    p->data[1] = 0xDE;
    p->data[2] = 0xFF;
    p->data[3] = 0x01;

    struct s *testCmd1 = (struct s *) &p->data;
    struct s *testCmd2 = (struct s *) p->data;
    printf("0x%02x 0x%02x  0x%02x\n", p->data[0], testCmd1->cmd, testCmd2->cmd);
    printf("0x%02x 0x%02x  0x%02x\n", p->data[1], testCmd1->main, testCmd2->main);
    printf("0x%02x 0x%02x  0x%02x\n", p->data[2], testCmd1->sub, testCmd2->sub);
    printf("0x%02x 0x%02x  0x%02x\n", p->data[3], testCmd1->index, testCmd2->index);    
    return 0;
}

Running the code above prints out:

0xbc 0xbc 0xbc

0xde 0xde 0xde

0xff 0xff 0xff

0x01 0x01 0x01

I am wondering why &p->data and p->data seem to get resolved to the same address.

It seems to me like &p->data should be a pointer to the address of data[0], while p->data would be simply the address of data[0]. I would get weird values printing out for one of them if that were the case though, correct?

Ideally, I don't think I would use code like this, but I ran across it in someone elses code and this was a test I wrote to see what was going on.

If this question has already been answered, I couldn't find it, apologies if that is the case.

Answering my own question after going though the posts commented by Étienne:

"The address of an array is the same as the address of the first element"

-John Bode (from Address of array - difference between having an ampersand and no ampersand )

ie for an array named "data": &data == data

So in my case, &p->data is the same address as p->data.

Thanks for the quick response, Étienne!

p->data means &p->data[0] when used in a value context, since it is the name of an array. This is a pointer to the first element of the array.

&p->data is a pointer to the whole array. The operation whereby the array name gets converted to a pointer to the first element is suppressed when the name is used with & or sizeof .

The whole array starts at the same memory location as the first element of the array, which is why these pointers both contain the same memory address when converted to a common type.

NB. It is not a standard requirement that both pointers use the same internal representation, although almost all systems would do so.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM