'struct' 之前的预期表达式 - 错误

Expected expression before 'struct' - Error

我正在使用 CrossStudio IDE 制作一个简单的函数原型,为 STM32F30x ARM 处理器初始化 UART 波特率和其他参数。函数原型的目标只是打印初始化外设 (stm32f30x.c) 的波特率,因此我期望为“9600”。相反,错误 "expected expression before 'USART_Init'" 返回。共有3个文件:

1) config_uart.c -> Contains the function prototype
2) stm32f30x_usart.c -> Contains Initializing function, which I call from config_uart.c
3) stm32f30x_usart.h -> Contains a prototype for struct definition

Config_uart.c正文

void Comm_Initial (void) {
    typedef struct USART_Init USART_Init;
    void USART_StructInit(USART_InitStruct);
    printf("%d\n", USART_Init->USART_BaudRate);
}

来自 stm32f30x_usart.c 正文

void USART_StructInit(USART_InitTypeDef* USART_InitStruct)
{
    /* USART_InitStruct members default value */
    USART_InitStruct->USART_BaudRate = 9600;
    USART_InitStruct->USART_WordLength = USART_WordLength_8b;
    USART_InitStruct->USART_StopBits = USART_StopBits_1;
    USART_InitStruct->USART_Parity = USART_Parity_No ;
    USART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None;
}

来自 stm32f30x_usart.h 正文

typedef struct
{
    uint32_t USART_BaudRate;
    uint32_t USART_WordLength;
    uint32_t USART_StopBits;
    uint32_t USART_Parity;
    uint32_t USART_Mode;
    uint32_t USART_HardwareFlowControl;
} USART_InitTypeDef;

我不确定错误出在哪里,因为我尝试了多种变体来尝试找出问题,这是我得到的最接近的结果。我不明白什么?感谢任何帮助。

void Comm_Initial (void) {
    typedef struct USART_Init USART_Init;

我没有在任何地方看到定义的类型 struct USART_Init。 typedef 仍然允许,但它指的是不完整的类型。

您确实有一个名为 USART_InitTypeDef 的类型,它有一个名为 USART_BaudRate 的成员。

    void USART_StructInit(USART_InitStruct);
    printf("%d\n", USART_Init->USART_BaudRate);
}

USART_Init 是类型名称。 -> 运算符需要结构指针或联合指针类型的 表达式 作为其左操作数。

如果您将 USART_Init 替换为 USART_InitTypeDef* 类型的某些表达式,那么您应该能够计算该表达式。

请注意 %d 需要类型为 int 的参数,而不是 uint32_t。有几种方法可以打印 uint32_t 值;最简单的是:

printf("%lu\n", (unsigned long)some_pointer->USART_BaudRate);