howto init 2d structure to zero ?

S

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.
 
B

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.
 
S

Stef

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.

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;
}
 
J

Jens.Toerring

Stef said:
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.
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};

Use instead

struct tab aaaa[MAXTAB][MAXCOL] = {{{0}}};

If you have e.g. a 2-dimensional arrays of ints you would do e.g.

int foo[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };

and since the initializer for a structure needs curly braces you need
another level of them for your 2-dimensional array of structures.

If the structure has more than a single member put in a value for
each member, i.e.

struct tab2 {
double freq;
int x;
}

struct tab2 ccc[MAXTAB][MAXCOL] = {{{0,0}}};

Regards, Jens
 
S

S.Tobias

Stef said:
For example with array I do.
int array[MAX] = {0}
but:
structure test bb[MAX][MAX] = {{0},{0}};
won't work.. ?

What makes you think it won't work, assuming all necessary
definitions are provided?

This compiled cleanly:
#define MAX 10
struct test { int x; };
struct test bb[MAX][MAX] = {{0},{0}};
 
S

Stef

Stef said:
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.
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};

Use instead

struct tab aaaa[MAXTAB][MAXCOL] = {{{0}}};

If you have e.g. a 2-dimensional arrays of ints you would do e.g.

int foo[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };

and since the initializer for a structure needs curly braces you need
another level of them for your 2-dimensional array of structures.

If the structure has more than a single member put in a value for
each member, i.e.

struct tab2 {
double freq;
int x;
}

struct tab2 ccc[MAXTAB][MAXCOL] = {{{0,0}}};

Regards, Jens

Ok thank yoo .. That worked !

Stef
 
S

Stef

Stef said:
For example with array I do.
int array[MAX] = {0}
but:
structure test bb[MAX][MAX] = {{0},{0}};
won't work.. ?

What makes you think it won't work, assuming all necessary
definitions are provided?

This compiled cleanly:
#define MAX 10
struct test { int x; };
struct test bb[MAX][MAX] = {{0},{0}};

~: cat strct.c
#include <stdio.h>

#define MAX 10

int main(void) {
struct test { int x; };
struct test bb[MAX][MAX] = {{0},{0}};

return 0;
}

~: gcc -Wall -pedantic strct.c -o strct
strct.c: In function `main':
strct.c:7: warning: missing braces around initializer
strct.c:7: warning: (near initialization for `bb[0][0]')
strct.c:7: warning: unused variable `bb'

That made me thinc it won't worked

Stef.
 
S

S.Tobias

Stef said:
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.
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;
}

The program is correct. Compiler may issue as many warnings it
feels like to. The extra warnings that you see are invoked
by "-Wall" option to gcc (since we already have "-pedantic", this
one could be called "-paranoic"); look into the gcc documentation.
 
S

Stef

Stef said:
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.
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;
}

The program is correct. Compiler may issue as many warnings it
feels like to. The extra warnings that you see are invoked
by "-Wall" option to gcc (since we already have "-pedantic", this
one could be called "-paranoic"); look into the gcc documentation.

I know what flags I am passing, even tho the program might be correct
I still feel it's proper custom to have code compiled cleanly without
any warnings..

Stef
 
F

Flash Gordon

Stef said:
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.. ?

You can just use:
structure test bb[MAX][MAX] = {0};
 
M

Michael Knaup

Stef said:
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.
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};

Use instead

struct tab aaaa[MAXTAB][MAXCOL] = {{{0}}};

If you have e.g. a 2-dimensional arrays of ints you would do e.g.

int foo[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };

and since the initializer for a structure needs curly braces you need
another level of them for your 2-dimensional array of structures.

If the structure has more than a single member put in a value for
each member, i.e.

struct tab2 {
double freq;
int x;
}

struct tab2 ccc[MAXTAB][MAXCOL] = {{{0,0}}};

Regards, Jens
Even int the case of a struct with more than one component,
this should be enough to init the whole array to "zero"

struct tab2 ccc[MAXTAB][MAXCOL] = {{{ 0 }}};
 
F

Flash Gordon

Stef said:
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.

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'

The compiler is allowed to warn about anything it wants to, even
perfectly correct C. If you are using gcc you can disable the warning,
but the details of gcc are off topic here.
 
S

S.Tobias

Stef said:
On 24 May 2005 11:14:49 GMT, "S.Tobias"
I know what flags I am passing, even tho the program might be correct
I still feel it's proper custom to have code compiled cleanly without
any warnings..

I agree. But there must be some limit to the warnings level beyond
which they don't help you write better code, but just annoy you and
force you to keep unnecessary clutter.
I have tried to use "-Wall" for some time, but dropped it, because
it didn't give me any useful information.
For example, I often use this idiom:
struct object { /*...*/ };
const struct object object_zero = {0}; /* a typed "constant" */
which is perfectly correct and clean code. With "-Wall" I would
have to write:
const struct object object_zero = {{{0,{0,{0,0},0},{0,{0,0}},0},.....etc}};
which doesn't look as good for me.
(There were also other reasons to get rid of "-Wall".)
 
C

Chris Torek

... I still feel it's proper custom to have code compiled
cleanly without any warnings..

I agree with this sentiment, and in this case the "fix" was
clear enough (put in the right number of braces), but consider:
what will you do if you need to compile with both Compiler A
*and* Compiler B, and code of the form:

void f(short s) {
...
}

produces:

warning: function f will not be compatible with K&R C

from compiler A, so you try instead:

void f(int s0) {
short s = s0;
...
}

which produces:

warning: assignment to s may truncate s0

from Compiler B, so you try something else (such as a cast or a
mask), but no matter what you do, you always get at least one
warning from at least one of the two compilers?

In extreme cases, you might even find that a single compiler produces
a warning no matter what you do. Sometimes you have to live with
warnings. :)

Ideally, there is some portable and clear way to write the code
that is warning-free, but sometimes "there just ain't". Occasionally
one can get rid of a warning by writing nonportable, convoluted,
fragile code instead of portable, simple, robust code; in this
case, eliminating the warning is not a good idea after all.
 
S

Stef

I agree with this sentiment, and in this case the "fix" was
clear enough (put in the right number of braces), but consider:
what will you do if you need to compile with both Compiler A
*and* Compiler B, and code of the form:

void f(short s) {
...
}

produces:

warning: function f will not be compatible with K&R C

from compiler A, so you try instead:

void f(int s0) {
short s = s0;
...
}

which produces:

warning: assignment to s may truncate s0

from Compiler B, so you try something else (such as a cast or a
mask), but no matter what you do, you always get at least one
warning from at least one of the two compilers?

In extreme cases, you might even find that a single compiler produces
a warning no matter what you do. Sometimes you have to live with
warnings. :)

Ideally, there is some portable and clear way to write the code
that is warning-free, but sometimes "there just ain't". Occasionally
one can get rid of a warning by writing nonportable, convoluted,
fragile code instead of portable, simple, robust code; in this
case, eliminating the warning is not a good idea after all.

Roger.. And thank yoo for the complete answer.

Stef
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top