473,396 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

howto init 2d structure to zero ?

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.
Nov 14 '05 #1
14 2312
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

Nov 14 '05 #2
On 24 May 2005 03:32:09 -0700, "bjrnove" <bj*****@gmail.com> wrote:
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;
}
Nov 14 '05 #3
Stef <St*****@sirtakie.tk> wrote:
On 24 May 2005 03:32:09 -0700, "bjrnove" <bj*****@gmail.com> wrote:
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
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #4
Stef <St*****@sirtakie.tk> wrote:
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}};

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 14 '05 #5
On 24 May 2005 10:54:17 GMT, Je***********@physik.fu-berlin.de wrote:
Stef <St*****@sirtakie.tk> wrote:
On 24 May 2005 03:32:09 -0700, "bjrnove" <bj*****@gmail.com> wrote:

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
Nov 14 '05 #6
Stef <St*****@sirtakie.tk> wrote:
On 24 May 2005 03:32:09 -0700, "bjrnove" <bj*****@gmail.com> wrote:
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.

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 14 '05 #7
On 24 May 2005 11:06:31 GMT, "S.Tobias"
<si***@FamOuS.BedBuG.pAlS.INVALID> wrote:
Stef <St*****@sirtakie.tk> wrote:
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.
Nov 14 '05 #8
On 24 May 2005 11:14:49 GMT, "S.Tobias"
<si***@FamOuS.BedBuG.pAlS.INVALID> wrote:
Stef <St*****@sirtakie.tk> wrote:
On 24 May 2005 03:32:09 -0700, "bjrnove" <bj*****@gmail.com> wrote:

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
Nov 14 '05 #9
Je***********@physik.fu-berlin.de wrote:
Stef <St*****@sirtakie.tk> wrote:
On 24 May 2005 03:32:09 -0700, "bjrnove" <bj*****@gmail.com> wrote:

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

--
Michael Knaup
Nov 14 '05 #10
Stef <St*****@sirtakie.tk> wrote:
On 24 May 2005 11:14:49 GMT, "S.Tobias"
<si***@FamOuS.BedBuG.pAlS.INVALID> wrote:
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..


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".)

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 14 '05 #11
Stef wrote:
On 24 May 2005 03:32:09 -0700, "bjrnove" <bj*****@gmail.com> wrote:
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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #12
Stef wrote:
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};
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #13
In article <m2********************************@4ax.com>
Stef <St*****@sirtakie.tk> wrote:
... 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.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #14
On 24 May 2005 16:29:10 GMT, Chris Torek <no****@torek.net> wrote:
In article <m2********************************@4ax.com>
Stef <St*****@sirtakie.tk> wrote:
... 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.


Roger.. And thank yoo for the complete answer.

Stef
Nov 14 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: prettysmurfed | last post by:
Hi all I have a bit of a problem, the subject of this post is almost selfexplaing. But here goes: Heres an example of the code I want to implement, its all nice and simple, but the flaw is I...
6
by: stuie_norris | last post by:
Hi Group, I am trying to define a strucutre of constants. Then switch on this structure of constant values. In the following example of my problem the code fails to compile due to: case...
29
by: Ancient_Hacker | last post by:
It sure would be nice if I could have a macro that add a level of indirection to its argument. So if I write: AddIndirection( X ) The macro AddIndirection will do: #define X (*X) ...
6
by: Ian Boyd | last post by:
Every time during development we had to make table changes, we use Control Center. Most of the time, Control Center fails. If you try to "undo all", it doesn't, and you end up losing your identity...
12
by: Stef Mientki | last post by:
In the example below, "pin" is an object with a number of properties. Now I want 1- an easy way to create objects that contains a number of these "pin" 2- an multiple way to access these "pin",...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
4
by: maborak | last post by:
loading.............. var a =function() { this.b=function(){}; } //howto isset this.b ?
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
11
by: michelqa | last post by:
Hello, I can retrieve column text from a ListView in another process but I cant figure out how to access to structure elements (LVCOLUMN) <code> //Handle variable is a valid ListView handle ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.