将struct fileds初始化为零 [英] Initialize struct fileds to zero
问题描述
大家好,
以下程序是使用带有-W的gcc编译的。选项。 GCC是
给出以下警告信息
initStructElem.c:在函数main中:
initStructElem.c: 20:警告:缺少初始化程序
initStructElem.c:20:警告:(接近初始化为`str1.f'')
这是程序
1 #include< stdio.h>
2
3 int main(无效)
4 {
5 typedef struct
6 {
7 int i1;
8 float f1;
9} str_1t;
10
11 typedef struct
12 {
13 int i;
14浮动f;
15 char c;
16 int * pi;
17 str_1t s2 ;
18} str_t;
19
20 str_t str1 = {0};
21 >
22 printf(" str1.i =%d \t str1.f =%f \ t str1.c =%d \ t
str1.pi =% p \ n",str1.i,str1 .f,str1.c,str1.pi);
23 printf (" str1.s2.i1 =%d \ n",str1.s2.i1);
24返回0;
25}
请告诉我为什么gcc会抱怨。
这不是初始化结构字段的标准吗?
Hi all,
The following program is compiled using gcc with "-W" option. GCC is
giving the following warning message
initStructElem.c: In function `main'':
initStructElem.c:20: warning: missing initializer
initStructElem.c:20: warning: (near initialization for `str1.f'')
Here is the program
1 #include <stdio.h>
2
3 int main(void)
4 {
5 typedef struct
6 {
7 int i1;
8 float f1;
9 }str_1t;
10
11 typedef struct
12 {
13 int i;
14 float f;
15 char c;
16 int *pi;
17 str_1t s2;
18 }str_t;
19
20 str_t str1={0};
21
22 printf("str1.i=%d\t str1.f=%f\t str1.c=%d\t
str1.pi=%p\n",str1.i,str1 .f,str1.c,str1.pi);
23 printf("str1.s2.i1=%d\n",str1.s2.i1);
24 return 0;
25 }
Please let me know why gcc is complaining.
Is this not a standard of way of intializing structure fields?
推荐答案
va******@rediffmail.com ??? ?????
va******@rediffmail.com ???é?????
大家好,
以下程序是使用带有-W的gcc编译的。选项。 GCC发出以下警告信息
initStructElem.c:在函数`main''中:
initStructElem.c:20:警告:缺少初始化程序
initStructElem。 c:20:警告:(接近初始化为'str1.f'')
这是程序
1 #include< stdio.h>
2
3 int main(void)
4 {
5 typedef struct
6 {
7 int i1;
8 float f1;
9} str_1t;
10
11 typedef struct
12 {
13 int i;
14 float f;
15 char c;
16 int * pi;
17 str_1t s2;
18} str_t;
19
20 str_t str1 = {0};
21
22 printf( " str1.i =%d \t str1.f =%f \t str1.c =%d \ t
str1.pi =%p \ n",str1.i,str1。 f,str1.c,str1.pi);
23 printf(" str1.s2.i1 =%d \\ t n",str1.s2.i1);
24返回0;
25}
请让我知道gcc为什么抱怨。
这不是初始化的标准结构字段?
Hi all,
The following program is compiled using gcc with "-W" option. GCC is
giving the following warning message
initStructElem.c: In function `main'':
initStructElem.c:20: warning: missing initializer
initStructElem.c:20: warning: (near initialization for `str1.f'')
Here is the program
1 #include <stdio.h>
2
3 int main(void)
4 {
5 typedef struct
6 {
7 int i1;
8 float f1;
9 }str_1t;
10
11 typedef struct
12 {
13 int i;
14 float f;
15 char c;
16 int *pi;
17 str_1t s2;
18 }str_t;
19
20 str_t str1={0};
21
22 printf("str1.i=%d\t str1.f=%f\t str1.c=%d\t
str1.pi=%p\n",str1.i,str1 .f,str1.c,str1.pi);
23 printf("str1.s2.i1=%d\n",str1.s2.i1);
24 return 0;
25 }
Please let me know why gcc is complaining.
Is this not a standard of way of intializing structure fields?
问题是int * pi;,它是指针,你必须初始化它
使用之前!
the problem is the "int *pi;",it''s a pointer ,you must intializing it
before use it !
va ****** @ rediffmail.com 写道:
大家好,
以下程序是使用带有-W的gcc编译的。选项。 GCC发出以下警告信息
initStructElem.c:在函数`main''中:
initStructElem.c:20:警告:缺少初始化程序
initStructElem。 c:20:警告:(接近初始化为'str1.f'')
这是程序
[snip] 5 typedef struct
6 {
7 int i1;
8 float f1;
9} str_1t;
10
11 typedef struct
12 {
13 int i;
14 float f;
15 char c;
16 int * pi;
17 str_1t s2;
18} str_t;
19
20 str_t str1 = {0};
[snip]
请告诉我为什么gcc在抱怨。
因为你已经在supercalifragiliantant模式下调用了它。
它希望你为所有结构提供显式初始化器
成员:
str_t str1 = {0,0,0,0,0,0};
(即:str1.i,str1.f,str1.c, str1.pi,str1.s2.i1,str1.s2.f1)。
在编译器文档中查看`-W'选项的作用。
是这不是初始化结构字段的标准方法吗?
Hi all,
The following program is compiled using gcc with "-W" option. GCC is
giving the following warning message
initStructElem.c: In function `main'':
initStructElem.c:20: warning: missing initializer
initStructElem.c:20: warning: (near initialization for `str1.f'')
Here is the program
[snip] 5 typedef struct
6 {
7 int i1;
8 float f1;
9 }str_1t;
10
11 typedef struct
12 {
13 int i;
14 float f;
15 char c;
16 int *pi;
17 str_1t s2;
18 }str_t;
19
20 str_t str1={0}; [snip]
Please let me know why gcc is complaining.
Because you''ve invoked it in supercalifragilipedantic mode.
It wants you to give explicit initializers for all struct
members:
str_t str1={0,0,0,0,0,0};
(ie.: str1.i, str1.f, str1.c, str1.pi, str1.s2.i1, str1.s2.f1).
Have a look in the compiler docs what `-W'' option does.
Is this not a standard of way of intializing structure fields?
str_t str1 = {0};
完全是标准和定义。
[OT]也许你想要`-Wall -pedantic''选项而不是`-W''。
-
Stan Tobias
mailx`echo si***@FamOuS.BedBuG.pAlS.INVA LID | sed s / [[:upper:]] // g`
str_t str1={0};
is perfectly stardard and defined.
[OT] Perhaps you want `-Wall -pedantic'' options instead of `-W''.
--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
感谢Tobias的回复。
Thanks for the reply Tobias.
也许你想要`-Wall -pedantic''选项而不是'-W''。
Perhaps you want `-Wall -pedantic'' options instead of `-W''.
我必须使用-W选项进行编译,因为这是其中一个
来自我的客户的
要求。还有其他方法可以将所有大型结构的b $ b $字段初始化为0吗?
I have to compile using -W option because this is one of the
requirement from my client. Is there other way of intializing all the
fields of big structures to 0?
这篇关于将struct fileds初始化为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!