error: too many initializers when initializing an array of strucures.

M

mukesh

Hi,

compilation of below code results in error "too many initailizer for
s3".

what could be the possible cause ?

Thanks,
Mukesh

++++++++++++++++++++++++++++++++++++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
int n1;
char c;
}s1;

typedef struct{
int n2;
s1 s1v[20];
}s2;

typedef struct {
int n3;
s2 s2v[20];
}s3;


static s3 val[] =
{
{1,
{20,
{
{1, 12},
{1, 12}
}
}
,{20,
{
{1, 12},
{1, 12}
}
},

},
{2,
{20,
{
{1, 12},
{1, 12}}
}
}
};

int main()
{

printf("\n hello world\n");

return 0;
}
++++++++++++++++++++++++++++++++++++
 
G

Giacomo Degli Esposti

Hi,

compilation of below code results in error "too many initailizer for
s3".

 what could be the possible cause ?

You missed a couple of { }
static s3 val[] =
    {
        {1,

/* here you have an array of s2, put a { */
{
            {20,
                {
                    {1, 12},
                    {1, 12}
                }
             }
            ,{20,
                {
                    {1, 12},
                    {1, 12}
                }
             },

/* end of array */
}
        },
        {2,
[...]

ciao
Giacomo
 
M

mukesh

compilation of below code results in error "too many initailizer for
s3".
 what could be the possible cause ?

You missed a couple of { }
static s3 val[] =
    {
        {1,

/* here you have an array of s2, put a { */
            {
            {20,
                {
                    {1, 12},
                    {1, 12}
                }
             }
            ,{20,
                {
                    {1, 12},
                    {1, 12}
                }
             },

/* end of array */
             }
        },
        {2,

[...]

ciao
Giacomo

Thanks...!

Yes i missed a {. after changing it compiles fine.

-Mukesh
 
M

mohangupta13

You missed a couple of { }
static s3 val[] =
{
{1,
/* here you have an array of s2, put a { */
{
/* end of array */
}

ciao
Giacomo

Thanks...!

Yes i missed a {. after changing it compiles fine.

-Mukesh

I have one more question by this type of initialization are the rest
of the elements that's not initialized here set to zero?
As the structure s2 has an array of s1 of size 20 but only two of them
is initialized are the rest sent to zero automatically??

Mohan
 
J

James Kuyper

mohangupta13 wrote:
....
I have one more question by this type of initialization are the rest
of the elements that's not initialized here set to zero?
As the structure s2 has an array of s1 of size 20 but only two of them
is initialized are the rest sent to zero automatically??

Yes, and yes.
 
M

mukesh

Hi,
compilation of below code results in error "too many initailizer for
s3".
 what could be the possible cause ?
You missed a couple of { }
static s3 val[] =
    {
        {1,
/* here you have an array of s2, put a { */
            {
            {20,
                {
                    {1, 12},
                    {1, 12}
                }
             }
            ,{20,
                {
                    {1, 12},
                    {1, 12}
                }
             },
/* end of array */
             }
        },
        {2,
[...]
ciao
Giacomo
Thanks...!

Yes i missed a {. after changing it compiles fine.

I have one more question by this type of initialization are the rest
of the elements that's not initialized here set to zero?
As the structure s2 has an array of s1 of size 20 but only two of them
is initialized are the rest sent to zero automatically??

Mohan- Hide quoted text -

- Show quoted text -

Yes...

since the storage class is static, so by default it will initialize
the rest of the elements with 0.
 
M

mohangupta13

On May 7, 5:51 pm, Giacomo Degli Esposti
Hi,
compilation of below code results in error "too many initailizer for
s3".
what could be the possible cause ?
You missed a couple of { }
static s3 val[] =
{
{1,
/* here you have an array of s2, put a { */
{
{20,
{
{1, 12},
{1, 12}
}
}
,{20,
{
{1, 12},
{1, 12}
}
},
/* end of array */
}
},
{2,
[...]
ciao
Giacomo
Thanks...!
Yes i missed a {. after changing it compiles fine.
-Mukesh
I have one more question by this type of initialization are the rest
of the elements that's not initialized here set to zero?
As the structure s2 has an array of s1 of size 20 but only two of them
is initialized are the rest sent to zero automatically??
Mohan- Hide quoted text -
- Show quoted text -

Yes...

since the storage class is static, so by default it will initialize
the rest of the elements with 0.

oh i missed "static"
thanx...
 
J

jameskuyper

mukesh said:
On May 7, 5:51 pm, Giacomo Degli Esposti
static s3 val[] =
{
{1,
....
I have one more question by this type of initialization are the rest
of the elements that's not initialized here set to zero?
As the structure s2 has an array of s1 of size 20 but only two of them
is initialized are the rest sent to zero automatically??
....
Yes...

since the storage class is static, so by default it will initialize
the rest of the elements with 0.

The storage class is irrelevant; if an initializer list is provided,
"all subobjects that are not initialized explicitly shall be
initialized implicitly the same as objects that have static storage
duration." (6.7.8p19)
 

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,773
Messages
2,569,594
Members
45,121
Latest member
LowellMcGu
Top