Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > howto init 2d structure to zero ?

Reply
Thread Tools

howto init 2d structure to zero ?

 
 
Stef
Guest
Posts: n/a
 
      05-24-2005
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.
 
Reply With Quote
 
 
 
 
bjrnove
Guest
Posts: n/a
 
      05-24-2005
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

 
Reply With Quote
 
 
 
 
Stef
Guest
Posts: n/a
 
      05-24-2005
On 24 May 2005 03:32:09 -0700, "bjrnove" <(E-Mail Removed)> 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;
}
 
Reply With Quote
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      05-24-2005
Stef <(E-Mail Removed)> wrote:
> On 24 May 2005 03:32:09 -0700, "bjrnove" <(E-Mail Removed)> 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 ___ http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
S.Tobias
Guest
Posts: n/a
 
      05-24-2005
Stef <(E-Mail Removed)> 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 http://www.velocityreviews.com/forums/(E-Mail Removed)LID | sed s/[[:upper:]]//g`
 
Reply With Quote
 
Stef
Guest
Posts: n/a
 
      05-24-2005
On 24 May 2005 10:54:17 GMT, (E-Mail Removed)-berlin.de wrote:

> Stef <(E-Mail Removed)> wrote:
> > On 24 May 2005 03:32:09 -0700, "bjrnove" <(E-Mail Removed)> 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
 
Reply With Quote
 
Stef
Guest
Posts: n/a
 
      05-24-2005
On 24 May 2005 11:06:31 GMT, "S.Tobias"
<(E-Mail Removed)> wrote:

> Stef <(E-Mail Removed)> 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.
 
Reply With Quote
 
S.Tobias
Guest
Posts: n/a
 
      05-24-2005
Stef <(E-Mail Removed)> wrote:
> On 24 May 2005 03:32:09 -0700, "bjrnove" <(E-Mail Removed)> 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 (E-Mail Removed)LID | sed s/[[:upper:]]//g`
 
Reply With Quote
 
Stef
Guest
Posts: n/a
 
      05-24-2005
On 24 May 2005 11:14:49 GMT, "S.Tobias"
<(E-Mail Removed)> wrote:

> Stef <(E-Mail Removed)> wrote:
> > On 24 May 2005 03:32:09 -0700, "bjrnove" <(E-Mail Removed)> 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
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      05-24-2005
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
init of class members : mem(0) vs. mem() vs. not-init at all news.aon.at C++ 11 01-29-2011 07:30 PM
Synplify - Init Rom from file - Howto? dkbauter VHDL 1 08-04-2009 11:25 AM
questions about object initialization, default-init and value-init Jess C++ 4 05-04-2007 02:47 AM
Sequence Order between Page Init and User Control Init Tony Cheng ASP .Net 1 02-24-2006 01:56 PM
Compiler/Linker Error undefined reference to 'std::ios_base::Init::Init[in-charge]() clusardi2k@aol.com C++ 1 08-18-2005 07:11 PM



Advertisments