gpt4 book ai didi

c - 数据定义没有类型或存储类

转载 作者:太空宇宙 更新时间:2023-11-04 00:26:27 25 4
gpt4 key购买 nike

我试图将存储分配给结构指针。这是简化的代码 - 你能告诉我我做错了什么吗?

可以.h

#ifndef CAN_H_
#define CAN_H_

typedef struct {
u32 id;
u32 mask;
char length;
char data[8];
char status;
} CAN_PACKET;

项目.c

#include "can.h"

CAN_PACKET *packet_Tx, *packet_Rx;
CAN_PACKET packet1, packet2;
packet_Tx = (CAN_PACKET*)malloc(sizeof(CAN_PACKET)); /* line 20 in text */
packet_Tx = &packet1;
packet_Rx = &packet2;

int main(void)
{
while(1)
{
}
}

我收到这些错误/警告:

Warning 1 data definition has no type or storage class c:\users\blu\documents\avrstudio\Can5\Can5\Can5.c 20 2 Can5

Warning 2 type defaults to 'int' in declaration of 'packet_Tx' c:\users\blu\documents\avrstudio\Can5\Can5\Can5.c 20 2 Can5

Error 3 conflicting types for 'packet_Tx' c:\users\blu\documents\avrstudio\Can5\Can5\Can5.c 20 2 Can5

Error 4 previous declaration of 'packet_Tx' was here c:\users\blu\documents\avrstudio\Can5\Can5\Can5.c 18 13 Can5

Warning 5 initialization makes integer from pointer without a cast c:\users\blu\documents\avrstudio\Can5\Can5\Can5.c 20 14 Can5

当 packet_Tx 分配行被注释掉时,其余代码符合要求。非常感谢大家。

最佳答案

在 C 语言中,您不能在函数之外添加代码。移动这两行:

packet_Tx = (CAN_PACKET*)malloc(sizeof(CAN_PACKET));  
packet_Tx = &packet1; packet_Rx = &packet2;

main() 方法的顶部:

int main(void)
{
packet_Tx = (CAN_PACKET*)malloc(sizeof(CAN_PACKET));
packet_Tx = &packet1; packet_Rx = &packet2;

关于c - 数据定义没有类型或存储类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9403768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com