C语言 将结构成员复制到新结构中



我有一个概述如下的结构:

typedef struct {
/* Device */
UART_DEV uartDev;
/* gpio */
uint32_t gpiopio;
} DEVICE;

在main.c中,我声明了一个新的uart设备,并将其传递给一个名为configure((的函数;

/* main.c */
static UART_DEV newUartDev;
configure(newUartDev);

configure()位于test.c中,在配置功能中,我想创建uart设备的副本并将其存储在全局内存中。

/* Test.c */
UART_DEV globalUartDev;
DEVICE globalDevice;
static int configure(UART_DEV newUartDev) {
/* Create a global copy of spi device */
globalUartDev = newUartDev;
DEVICE.uartDev = globalUartDev;
}

然后我在main.c中调用另一个函数setTestsetTest位于test.c中。我不传递在main.c中声明的uart设备,而是只想使用我之前创建的全局结构。setTest调用 dev.c 中的open

/* test.c */
setTest() {
open(&globalDevice);
}

最后,我将在开放中使用uart设备。

/* dev.c */
int  open(DEVICE *dev) {
read(dev->uartDev)
}

这是创建副本并将其传递文件的正确方法吗?

DEVICE.uartDev = globalUartDev;  

DEVICE不是一个对象,而是一个结构。globalDevice是一个对象。所以应该是

globalDevice.uartDev = globalUartDev; 

最新更新