将数组转换为struct指针。

时间:2022-09-06 11:19:39
 #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

0 xbc 0 0 xbc xbc

0xde 0xde 0xde

xde 0 0 xde xde

0xff 0xff 0xff

0 xff 0 xff 0 xff

0x01 0x01 0x01

0 x01 0 x01 0 x01

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

我想知道为什么&p->数据和p->数据似乎被解析到同一个地址。

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?

在我看来,&p->数据应该是指向数据[0]地址的指针,而p->数据应该只是数据[0]的地址。如果是这样的话,我会打印出一些奇怪的值,对吧?

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.

理想情况下,我不认为我会使用这样的代码,但是我在elses代码中遇到了它,这是我编写的一个测试,看看发生了什么。

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

如果这个问题已经得到回答,我找不到,如果是这样的话,我很抱歉。

2 个解决方案

#1


1  

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)

- john Bode(来自数组地址——有一个&和没有&的区别)

i.e. for an array named "data": &data == data

例如,对于一个名为“data”的数组:&data ==数据。

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

在我的例子中,&p->数据与p->数据的地址相同。

Thanks for the quick response, Étienne!

谢谢你的快速回复,艾蒂安!

#2


0  

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->数据是指在值上下文中使用的&p->数据[0],因为它是数组的名称。这是指向数组第一个元素的指针。

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

数据是指向整个数组的指针。将数组名称转换为指向第一个元素的指针的操作在名称与&或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.

NB。这不是一个标准的要求,两个指针都使用相同的内部表示,尽管几乎所有的系统都这样做。

#1


1  

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)

- john Bode(来自数组地址——有一个&和没有&的区别)

i.e. for an array named "data": &data == data

例如,对于一个名为“data”的数组:&data ==数据。

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

在我的例子中,&p->数据与p->数据的地址相同。

Thanks for the quick response, Étienne!

谢谢你的快速回复,艾蒂安!

#2


0  

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->数据是指在值上下文中使用的&p->数据[0],因为它是数组的名称。这是指向数组第一个元素的指针。

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

数据是指向整个数组的指针。将数组名称转换为指向第一个元素的指针的操作在名称与&或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.

NB。这不是一个标准的要求,两个指针都使用相同的内部表示,尽管几乎所有的系统都这样做。