Connecting Tech Pros Worldwide Help | Site Map

array initialization standard

Stephen Mayes
Guest
 
Posts: n/a
#1: Nov 14 '05
I have initialized an array like this.

const char matrix[][] =
{
{0, 1, 2, 3},
{0, 1, 2},
{0, 1}
};

gcc, (with no options set,) errors unless I specify
const char matrix [][4] = ...
but at least one other compiler doesn't mind, and I'd rather not.

Can I assume the other compiler is divining the 4, and all members of matrix
will have length 4? If so, would this be an extension or a different
standard?

In either case, how would the unspecified members of matrix[1] and matrix[2]
be initialized?
Would referencing matrix[2][4], (not that I would,) be undefined?

I apologize for so many question marks in one post.


Stephen Mayes
Guest
 
Posts: n/a
#2: Nov 14 '05

re: array initialization standard



"Stephen Mayes" <stephen_del@themayeshouse.us> wrote in message
news:K4PHd.49817$Zv5.6226@bignews1.bellsouth.net.. .[color=blue]
>I have initialized an array like this.
>
> const char matrix[][] =
> {
> {0, 1, 2, 3},
> {0, 1, 2},
> {0, 1}
> };
>
> gcc, (with no options set,) errors unless I specify
> const char matrix [][4] = ...
> but at least one other compiler doesn't mind, and I'd rather not.
>
> Can I assume the other compiler is divining the 4, and all members of
> matrix will have length 4? If so, would this be an extension or a
> different standard?
>
> In either case, how would the unspecified members of matrix[1] and
> matrix[2] be initialized?
> Would referencing matrix[2][4], (not that I would,) be undefined?
>
> I apologize for so many question marks in one post.
>
>[/color]

OK. The compiler that allows the initialization without size subsequently
errors when I try to reference any members of matrix. 'unknown size of
incomplete array'...
So then I must specify the length of the largest array member?


pete
Guest
 
Posts: n/a
#3: Nov 14 '05

re: array initialization standard


Stephen Mayes wrote:[color=blue]
>
> "Stephen Mayes" <stephen_del@themayeshouse.us> wrote in message
> news:K4PHd.49817$Zv5.6226@bignews1.bellsouth.net.. .[color=green]
> >I have initialized an array like this.
> >
> > const char matrix[][] =
> > {
> > {0, 1, 2, 3},
> > {0, 1, 2},
> > {0, 1}
> > };
> >
> > gcc, (with no options set,) errors unless I specify
> > const char matrix [][4] = ...
> > but at least one other compiler doesn't mind, and I'd rather not.
> >
> > Can I assume the other compiler is divining the 4, and all members of
> > matrix will have length 4? If so, would this be an extension or a
> > different standard?
> >
> > In either case, how would the unspecified members of matrix[1] and
> > matrix[2] be initialized?
> > Would referencing matrix[2][4], (not that I would,) be undefined?
> >
> > I apologize for so many question marks in one post.
> >
> >[/color]
>
> OK. The compiler that allows the initialization without size subsequently
> errors when I try to reference any members of matrix. 'unknown size of
> incomplete array'...
> So then I must specify the length of the largest array member?[/color]

You can only leave the left most brackets empty in an array declaration.

int array1[] = {0};
int array2[][1] = {0};
int array3[][1][1] = {0};

--
pete
pete
Guest
 
Posts: n/a
#4: Nov 14 '05

re: array initialization standard


pete wrote:[color=blue]
>
> Stephen Mayes wrote:[color=green]
> >
> > "Stephen Mayes" <stephen_del@themayeshouse.us> wrote in message
> > news:K4PHd.49817$Zv5.6226@bignews1.bellsouth.net.. .[color=darkred]
> > >I have initialized an array like this.
> > >
> > > const char matrix[][] =
> > > {
> > > {0, 1, 2, 3},
> > > {0, 1, 2},
> > > {0, 1}
> > > };
> > >
> > > gcc, (with no options set,) errors unless I specify
> > > const char matrix [][4] = ...
> > > but at least one other compiler doesn't mind, and I'd rather not.
> > >
> > > Can I assume the other compiler is divining the 4, and all members of
> > > matrix will have length 4? If so, would this be an extension or a
> > > different standard?
> > >
> > > In either case, how would the unspecified members of matrix[1] and
> > > matrix[2] be initialized?
> > > Would referencing matrix[2][4], (not that I would,) be undefined?
> > >
> > > I apologize for so many question marks in one post.
> > >
> > >[/color]
> >
> > OK. The compiler that allows the initialization without size subsequently
> > errors when I try to reference any members of matrix. 'unknown size of
> > incomplete array'...
> > So then I must specify the length of the largest array member?[/color]
>
> You can only leave the left most brackets empty in an array
> declaration.[/color]

I suppose that should be "in an array initialization"
[color=blue]
>
> int array1[] = {0};
> int array2[][1] = {0};
> int array3[][1][1] = {0};
>
> --
> pete[/color]

--
pete
Mike Wahler
Guest
 
Posts: n/a
#5: Nov 14 '05

re: array initialization standard



"pete" <pfiland@mindspring.com> wrote in message
news:41F00916.5DF@mindspring.com...[color=blue]
> pete wrote:[color=green]
> >
> > Stephen Mayes wrote:[color=darkred]
> > >
> > > "Stephen Mayes" <stephen_del@themayeshouse.us> wrote in message
> > > news:K4PHd.49817$Zv5.6226@bignews1.bellsouth.net.. .
> > > >I have initialized an array like this.
> > > >
> > > > const char matrix[][] =
> > > > {
> > > > {0, 1, 2, 3},
> > > > {0, 1, 2},
> > > > {0, 1}
> > > > };
> > > >
> > > > gcc, (with no options set,) errors unless I specify
> > > > const char matrix [][4] = ...
> > > > but at least one other compiler doesn't mind, and I'd rather not.
> > > >
> > > > Can I assume the other compiler is divining the 4, and all members[/color][/color][/color]
of[color=blue][color=green][color=darkred]
> > > > matrix will have length 4? If so, would this be an extension or a
> > > > different standard?
> > > >
> > > > In either case, how would the unspecified members of matrix[1] and
> > > > matrix[2] be initialized?
> > > > Would referencing matrix[2][4], (not that I would,) be undefined?
> > > >
> > > > I apologize for so many question marks in one post.
> > > >
> > > >
> > >
> > > OK. The compiler that allows the initialization without size[/color][/color][/color]
subsequently[color=blue][color=green][color=darkred]
> > > errors when I try to reference any members of matrix. 'unknown size[/color][/color][/color]
of[color=blue][color=green][color=darkred]
> > > incomplete array'...
> > > So then I must specify the length of the largest array member?[/color]
> >
> > You can only leave the left most brackets empty in an array
> > declaration.[/color]
>
> I suppose that should be "in an array initialization"[/color]

Since we're being pedantic, make that
"array initializer", or "array initializer list".

-Mike


Closed Thread