howto init 2d结构为零? [英] howto init 2d structure to zero ?
问题描述
您好,
我有一个问题:
是否可以将二维结构数组初始化为零?
例如我使用数组。
int array [MAX] = {0}
但是:
结构测试bb [MAX] [MAX] = {{0},{0}};
不工作..?
谢谢你。
Stef。
Hello,
I have a question:
Is it possible to init a 2d array of structures to zero ?
For example with array I do.
int array[MAX] = {0}
but:
structure test bb[MAX][MAX] = {{0},{0}};
won''t work.. ?
thank yoo.
Stef.
推荐答案
嗨。
这应该工作:
struct test bb [MAX] [MAX] = {0};
如果你put = {0};作为初始化程序,它总是将
整个东西初始化为\0'。
-
bjrnove
Hi.
This should work thow:
struct test bb[MAX][MAX] = {0};
If you put = {0}; as the initializer it will alway initialize the
entire thing to \0''s.
--
bjrnove
2005年5月24日03:32:09 -0700," bjrnove" < BJ ***** @ gmail.com>写道:
On 24 May 2005 03:32:09 -0700, "bjrnove" <bj*****@gmail.com> wrote:
嗨。
这应该工作:
struct test bb [MAX] [MAX] = {0};
如果你把= {0};作为初始化程序,它将始终将整个事物初始化为\0'。
Hi.
This should work thow:
struct test bb[MAX][MAX] = {0};
If you put = {0}; as the initializer it will alway initialize the
entire thing to \0''s.
但编译器发出警告然后......如:
istrct.c:在函数`main'':
istrct.c:11:警告:初始化器周围缺少大括号
istrct.c: 11:警告:(接近初始化'aaaa [0]'')
istrct.c:12:警告:初始化程序周围缺少大括号
istrct.c:12:警告:(接近初始化`bbbb [0]'')
istrct.c:12:警告:未使用的变量`bbbb''
istrct.c:11:警告:未使用的变量`aaaa''
完整性这是一个小的简单骨架..
#include< stdio.h>
#define MAXTAB 100
#define MAXCOL 3
struct tab {
double freq; < br $>
};
int main(void){
struct tab aaaa [MAXTAB] [MAXCOL] = {0};
struct tab bbbb [MAXTAB] [MAXCOL] = {0};
返回0;
}
But the compiler gives warnings then... like:
istrct.c: In function `main'':
istrct.c:11: warning: missing braces around initializer
istrct.c:11: warning: (near initialization for `aaaa[0]'')
istrct.c:12: warning: missing braces around initializer
istrct.c:12: warning: (near initialization for `bbbb[0]'')
istrct.c:12: warning: unused variable `bbbb''
istrct.c:11: warning: unused variable `aaaa''
For completeness Here is small simple skeleton..
#include <stdio.h>
#define MAXTAB 100
#define MAXCOL 3
struct tab {
double freq;
};
int main(void) {
struct tab aaaa[MAXTAB][MAXCOL] = {0};
struct tab bbbb[MAXTAB][MAXCOL] = {0};
return 0;
}
Stef< St ***** @ sirtakie.tk>写道:
Stef <St*****@sirtakie.tk> wrote:
2005年5月24日03:32:09 -0700," bjrnove" < BJ ***** @ gmail.com>写道:
On 24 May 2005 03:32:09 -0700, "bjrnove" <bj*****@gmail.com> wrote:
嗨。
这应该工作:
struct test bb [MAX] [MAX] = {0};
如果你输入= {0};作为初始化程序,它将始终将整个事物初始化为\0'。
Hi.
This should work thow:
struct test bb[MAX][MAX] = {0};
If you put = {0}; as the initializer it will alway initialize the
entire thing to \0''s.
但是编译器发出警告然后......就像: br /> istrct.c:在函数main中:
istrct.c:11:警告:初始化程序周围缺少大括号
istrct.c:11:警告:(接近初始化为`aaaa [0 ]'')
istrct.c:12:警告:初始化程序周围缺少大括号
istrct.c:12:警告:(接近初始化为`bbbb [0]'')
istrct。 c:12:警告:未使用的变量`bbbb''
istrct.c:11:警告:未使用的变量`aaaa''
完整性这是一个小的简单骨架..
#include< ; stdio.h中>
#define MAXTAB 100
#define MAXCOL 3
struct tab {
double freq;
};
int main(void){
struct tab aaaa [MAXTAB] [MAXCOL] = {0};
struct tab bbbb [MAXTAB] [MAXCOL] = {0};
But the compiler gives warnings then... like:
istrct.c: In function `main'':
istrct.c:11: warning: missing braces around initializer
istrct.c:11: warning: (near initialization for `aaaa[0]'')
istrct.c:12: warning: missing braces around initializer
istrct.c:12: warning: (near initialization for `bbbb[0]'')
istrct.c:12: warning: unused variable `bbbb''
istrct.c:11: warning: unused variable `aaaa''
For completeness Here is small simple skeleton..
#include <stdio.h>
#define MAXTAB 100
#define MAXCOL 3
struct tab {
double freq;
};
int main(void) {
struct tab aaaa[MAXTAB][MAXCOL] = {0};
struct tab bbbb[MAXTAB][MAXCOL] = {0};
使用
struct tab aaaa [MAXTAB] [MAXCOL] = {{{0}}};
如果你有例如你会做的二维数组例如
int foo [2] [2] = {{1,2},{3,4}};
由于结构的初始化程序需要花括号,你需要
为你的二维结构数组提供另一个级别。
如果结构中有多个成员为每个成员加上一个值为
,即
struct tab2 {
double freq;
int x;
}
struct tab2 ccc [MAXTAB] [MAXCOL] = {{{ 0,0}}};
问候,Jens
-
\ Jens Thoms Toerring ___ 济*********** @ physik.fu-berlin.de
\ __________________________ http://www.toerring.de
这篇关于howto init 2d结构为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!