C 创建结构数组
为了解决这个问题,我一直在复制这个问题中给出的例子 现在我的代码如下所示:C 创建结构数组,c,arrays,struct,C,Arrays,Struct,为了解决这个问题,我一直在复制这个问题中给出的例子 现在我的代码如下所示: #define NUM_TRACES 6 typedef struct { uint32_t upstream_pin; uint32_t dnstream_pin; uint32_t led_pin; }trace; struct trace traces[NUM_TRACES]; traces[0] = {GPIO_Pin_3, GPIO_Pin_7 , GPIO_Pin_0}; tr
#define NUM_TRACES 6
typedef struct
{
uint32_t upstream_pin;
uint32_t dnstream_pin;
uint32_t led_pin;
}trace;
struct trace traces[NUM_TRACES];
traces[0] = {GPIO_Pin_3, GPIO_Pin_7 , GPIO_Pin_0};
traces[1] = {GPIO_Pin_4, GPIO_Pin_6 , GPIO_Pin_1};
但是我得到了以下错误
src/hal.c:17:14: error: array type has incomplete element type
struct trace traces[NUM_TRACES];
^
src/hal.c:19:1: warning: data definition has no type or storage class
traces[0] = {GPIO_Pin_3, GPIO_Pin_7 , GPIO_Pin_0};
我可以通过使数组成为指向跟踪结构的指针数组来修复第一个错误,我认为这是有意义的
struct trace* traces[NUM_TRACES];
但是这些行给出了一个错误
src/hal.c:19:1: warning: data definition has no type or storage class
traces[0] = {GPIO_Pin_3, GPIO_Pin_7 , GPIO_Pin_0};
^
src/hal.c:19:1: warning: type defaults to 'int' in declaration of 'traces'
src/hal.c:19:1: error: conflicting types for 'traces'
src/hal.c:17:15: note: previous declaration of 'traces' was here
struct trace* traces[NUM_TRACES];
^
src/hal.c:19:1: warning: excess elements in array initializer
traces[0] = {GPIO_Pin_3, GPIO_Pin_7 , GPIO_Pin_0};
^
我认为这是因为跟踪[0]实际上是存储数据地址的地方,而不是存储数据的地方?但我不知道如何更正此错误并将数据放置在数组中所需的位置。将结构重写为
struct trace
{
…
};
typedef struct trace
{
…
} trace;
现有代码的问题是您已经为引用该结构的trace
定义了一个匿名结构和一个typedef。这意味着没有类型struct trace
,因此您的行struct trace traces[NUM\u traces]
是指一个不存在的类型
您还可以将结构定义为
struct trace
{
…
};
typedef struct trace
{
…
} trace;
如果您愿意,这将为您提供跟踪
和结构跟踪
,但实际上没有任何意义
当然,您可以保留当前类型,只需说trace traces[NUM\u traces]
您的代码行
跟踪[0]={GPIO_Pin_3,GPIO_Pin_7,GPIO_Pin_0}代码>也不正确。编译器将其解释为没有类型的零长度数组的声明。如果要初始化跟踪
数组,必须在实际的初始值设定项中进行初始化,如中所示
struct trace traces[NUM_TRACES] = { {GPIO_Pin_3, GPIO_Pin_7 , GPIO_Pin_0} };
(如果NUM\u TRACES
大于1
这将使数组的其余部分保持零初始化)将结构重写为
struct trace
{
…
};
typedef struct trace
{
…
} trace;
现有代码的问题是您已经为引用该结构的trace
定义了一个匿名结构和一个typedef。这意味着没有类型struct trace
,因此您的行struct trace traces[NUM\u traces]
是指一个不存在的类型
您还可以将结构定义为
struct trace
{
…
};
typedef struct trace
{
…
} trace;
如果您愿意,这将为您提供跟踪
和结构跟踪
,但实际上没有任何意义
当然,您可以保留当前类型,只需说trace traces[NUM\u traces]
您的代码行跟踪[0]={GPIO_Pin_3,GPIO_Pin_7,GPIO_Pin_0}代码>也不正确。编译器将其解释为没有类型的零长度数组的声明。如果要初始化跟踪
数组,必须在实际的初始值设定项中进行初始化,如中所示
struct trace traces[NUM_TRACES] = { {GPIO_Pin_3, GPIO_Pin_7 , GPIO_Pin_0} };
(如果NUM\u trace
大于1
这将使数组的其余部分保持零初始化)您使用的是尚未提供定义的类型结构跟踪。您可以使用这种结构类型执行某些操作,但不需要系统知道其大小或成员
您所定义的是一个无标记的结构类型,由类型名trace
别名。由于该类型是无标记的,因此只能通过定义的别名引用该类型:
/* no struct */ trace traces[NUM_TRACES];
或者,如果您实际上使用tagtrace
声明了结构类型,则可以使用原始数组声明:
struct trace
{
uint32_t upstream_pin;
uint32_t dnstream_pin;
uint32_t led_pin;
} /* no instances declared here in this case */ ;
请特别注意,您不需要typedef
来声明结构类型;其目的是声明类型名别名。但是,您可以一次性定义标记的结构类型和别名,以便可以使用或不使用struct
关键字来引用它:
typedef struct trace
{
uint32_t upstream_pin;
uint32_t dnstream_pin;
uint32_t led_pin;
} trace ;
不过,我并不真的建议这样做——从风格上来说,最好选择一种形式来使用(struct
或aliased\u type\u name
),并坚持使用它。您使用的是尚未提供定义的类型struct trace
。您可以使用这种结构类型执行某些操作,但不需要系统知道其大小或成员
您所定义的是一个无标记的结构类型,由类型名trace
别名。由于该类型是无标记的,因此只能通过定义的别名引用该类型:
/* no struct */ trace traces[NUM_TRACES];
或者,如果您实际上使用tagtrace
声明了结构类型,则可以使用原始数组声明:
struct trace
{
uint32_t upstream_pin;
uint32_t dnstream_pin;
uint32_t led_pin;
} /* no instances declared here in this case */ ;
请特别注意,您不需要typedef
来声明结构类型;其目的是声明类型名别名。但是,您可以一次性定义标记的结构类型和别名,以便可以使用或不使用struct
关键字来引用它:
typedef struct trace
{
uint32_t upstream_pin;
uint32_t dnstream_pin;
uint32_t led_pin;
} trace ;
不过,我并不真的建议这样做——从风格上来说,最好选择一种形式来使用(struct
或aliased\u type\u name
),并坚持使用它。typedef struct{…}foo
foo something
,或struct foo{…}
struct foo something
。首先将foo
声明为匿名结构的别名。第二个声明一个命名的结构foo
。不要混搭。你能发布你的全部代码吗?这是一个问答网站,不是一个讨论论坛。如果您现在想用相同的代码询问不同的问题,请将其作为新问题发布——不要将问题编辑为完全不同的内容,尤其是在收到答案后。回滚。一旦你得到答案,你就不应该改变这个问题。这不是生活site@JohnBollinger:这就是回滚的目的。typedef struct{…}foo
=>foo something
,或struct foo{…}
=>struct foo something
。首先将foo
声明为匿名结构的别名。第二个声明一个命名的结构foo
。不要混搭。你能发布你的全部代码吗?这是一个问答网站,不是一个讨论论坛。如果您现在想用相同的代码询问不同的问题,请将其作为新问题发布——不要将问题编辑为完全不同的内容,尤其是在收到答案后。回滚。你是