将指针从一个结构转换为另一个结构有问题吗?

Problem with casting pointers from one structure to another structure?

我正在尝试将各个数组组件的地址转换为另一个结构

结构如下:

#define ADDRESS_SPACE 8

struct dma_engine
{
    int *Address[ADDRESS_SPACE] = {nullptr};
};

struct data_engine
{
    int data[ADDRESS_SPACE] = {0x10,0x14,0x18,0x1B,0x20,0x24,0x28,0x2B};
};

当我单独处理每个数组组件时它的工作原理如下:

dma_engine  Dma_0;
data_engine Data_0;

Dma_0.Address[0] = (int*) &Data_0.data[0];
Dma_0.Address[1] = (int*) &Data_0.data[1];
Dma_0.Address[2] = (int*) &Data_0.data[2];
Dma_0.Address[3] = (int*) &Data_0.data[3];
Dma_0.Address[4] = (int*) &Data_0.data[4];
Dma_0.Address[5] = (int*) &Data_0.data[5];
Dma_0.Address[6] = (int*) &Data_0.data[6];
Dma_0.Address[7] = (int*) &Data_0.data[7];

但是当我试图一次通过所有内容时,就像那样:

dma_engine  Dma_0;
data_engine Data_0;

Dma_0.Address = (int*) &Data_0.data;

我收到以下编译错误:

main.cpp: In function ‘int main(int, char**)’:
main.cpp:30:36: error: incompatible types in assignment of ‘int*’ to ‘int* [8]’
   30 |     Dma_0.Address = (int*) &Data_0.data;

此外,当我尝试这种方式时不起作用:

dma_engine  Dma_0;
data_engine Data_0;

Dma_0.Address = &Data_0.data;

我收到这个错误!

main.cpp: In function ‘int main(int, char**)’:
main.cpp:30:36: error: incompatible types in assignment of ‘int*’ to ‘int* [8]’
   30 |     Dma_0.Address = (int*) &Data_0.data;

有人可以帮我解决这个问题吗?

非常感谢

您需要将 Address 更改为指向 int[ADDRESS_SPACE] 的指针:

#define ADDRESS_SPACE 8

struct dma_engine {
    int (*Address)[ADDRESS_SPACE] = nullptr;  // correct type
};

struct data_engine {
    int data[ADDRESS_SPACE] = {0x10, 0x14, 0x18, 0x1B, 0x20, 0x24, 0x28, 0x2B};
};

int main() {
    dma_engine Dma_0;
    data_engine Data_0;

    Dma_0.Address = &Data_0.data; // no cast here
}

或者只是一个普通的 int*:

#define ADDRESS_SPACE 8

struct dma_engine {
    int *Address = nullptr;  // int*
};

struct data_engine {
    int data[ADDRESS_SPACE] = {0x10, 0x14, 0x18, 0x1B, 0x20, 0x24, 0x28, 0x2B};
};

int main() {
    dma_engine Dma_0;
    data_engine Data_0;

    Dma_0.Address = Data_0.data; // still no cast here, but don't take the address
}