微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

结构混乱

我有一段时间没有接触过C语言,所以我只是经历了一些概念,但却找不到任何有关结构的好资料.

任何人都可以解释

struct A
{
   int a;
   char b;
   float c;
};

这是结构A的声明或定义吗?

解决方法

这是一个宣言.

结构A;是一个forward declaration或不完整的声明.

struct A
{
   int a;
   char b;
   float c;
};

是完整的结构声明.

Example

另请查看comp.lang.c FAQ list Question 11.5

在向前声明struct之后,您可以使用结构指针,但不能取消引用指针或使用sizeof运算符或创建结构的实例.

声明后,您还可以使用struct对象,应用sizeof运算符等.

来自6.7.2.1 C11规范中的结构和联合说明符

8 The type is incomplete until immediately after the } that terminates
the list,and complete thereafter.

并从6.7.2.3标签

If a type specifier of the form
struct-or-union identifier occurs
other than as part of one of the above forms,and no other declaration
of the identifier as a tag is visible,then it declares an incomplete
structure or union type,and declares the identifier as the tag of
that type.131)
131A similar construction with enum does not exist

这不应该与extern struct A aa相混淆; v / s struct A aa = {/ *有些值* /};这是对象aa的声明和定义.

原文地址:https://www.jb51.cc/c/115695.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐