C语言结构体

loveqise 2016-11-12 09:31:57
代码如图,airline代表航线,plane代表飞机。一条航线有四个航班,为什么代码报错?
...全文
196 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-11-14
  • 打赏
  • 举报
回复
for (int i=0;i<12;i++) {
 plane[i].number=i+1;
 plane[i].mark=0;
}
“变量赋值”和“变量初始化”不是一回事! “变量赋值”发生在运行期,其写法遵循赋值语法规定。 “变量初始化”发生在编译期或运行期,其写法遵循初始化列表语法规定。
fefe82 2016-11-13
  • 打赏
  • 举报
回复
引用 9 楼 loveqise 的回复:
引用 8楼fefe82 的回复:
在 C 语言里,全局变量只能用常量初始化。
初始化了的结构体不算常量吗
不算。
6.6 Constant expressions
Syntax
1 constant-expression:
    conditional-expression

Description
2 A constant expression can be evaluated during translation rather than runtime, and
accordingly may be used in any place that a constant may be.

Constraints
3 Constant expressions shall not contain assignment, increment, decrement, function-call,
or comma operators, except when they are contained within a subexpression that is not
evaluated.
4 Each constant expression shall evaluate to a constant that is in the range of representable
values for its type.

Semantics
5 An expression that evaluates to a constant is required in several contexts. If a floating
expression is evaluated in the translation environment, the arithmetic range and precision
shall be at least as great as if the expression were being evaluated in the execution
environment.

6 An integer constant expression shall have integer type and shall only have operands
that are integer constants, enumeration constants, character constants, sizeof
expressions whose results are integer constants, _Alignof expressions, and floating
constants that are the immediate operands of casts. Cast operators in an integer constant
expression shall only convert arithmetic types to integer types, except as part of an
operand to the sizeof or _Alignof operator.

7 More latitude is permitted for constant expressions in initializers. Such a constant
expression shall be, or evaluate to, one of the following:
— an arithmetic constant expression,
— a null pointer constant,
— an address constant, or
— an address constant for a complete object type plus or minus an integer constant
expression.

8 An arithmetic constant expression shall have arithmetic type and shall only have
operands that are integer constants, floating constants, enumeration constants, character
constants, sizeof expressions whose results are integer constants, and _Alignof
expressions. Cast operators in an arithmetic constant expression shall only convert
arithmetic types to arithmetic types, except as part of an operand to a sizeof or
_Alignof operator.

9 An address constant is a null pointer, a pointer to an lvalue designating an object of static
storage duration, or a pointer to a function designator; it shall be created explicitly using
the unary & operator or an integer constant cast to pointer type, or implicitly by the use of
an expression of array or function type. The array-subscript [] and member-access .
and -> operators, the address & and indirection * unary operators, and pointer casts may
be used in the creation of an address constant, but the value of an object shall not be
accessed by use of these operators.

10 An implementation may accept other forms of constant expressions.

11 The semantic rules for the evaluation of a constant expression are the same as for
nonconstant expressions.
loveqise 2016-11-13
  • 打赏
  • 举报
回复
引用 8楼fefe82 的回复:
在 C 语言里,全局变量只能用常量初始化。
初始化了的结构体不算常量吗
loveqise 2016-11-12
  • 打赏
  • 举报
回复
引用 4楼赵4老师 的回复:
将name和subname删掉。
删掉也提示错误,而且删了结构就不完整了
赵4老师 2016-11-12
  • 打赏
  • 举报
回复
将name和subname删掉。
loveqise 2016-11-12
  • 打赏
  • 举报
回复
loveqise 2016-11-12
  • 打赏
  • 举报
回复
引用 1楼paschen 的回复:
没见到你plane的定义啊
有啊!
paschen 2016-11-12
  • 打赏
  • 举报
回复
没见到你plane的定义啊
fefe82 2016-11-12
  • 打赏
  • 举报
回复
在 C 语言里,全局变量只能用常量初始化。
loveqise 2016-11-12
  • 打赏
  • 举报
回复
引用 6楼wokaoyan1981 的回复:
你的结构体包含了四个字段,你只给前面两个int类型的字段初始化了,那后面的name和subname呢?
结构体不一定全部要初始化啊,只要部分有初始值就行
wokaoyan1981 2016-11-12
  • 打赏
  • 举报
回复
你的结构体包含了四个字段,你只给前面两个int类型的字段初始化了,那后面的name和subname呢?

69,368

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧