473,396 Members | 2,098 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.

array initialization standard

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

"Stephen Mayes" <st*********@themayeshouse.us> wrote in message
news:K4******************@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 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.


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?
Nov 14 '05 #2
Stephen Mayes wrote:

"Stephen Mayes" <st*********@themayeshouse.us> wrote in message
news:K4******************@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 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.


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?


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
Nov 14 '05 #3
pete wrote:

Stephen Mayes wrote:

"Stephen Mayes" <st*********@themayeshouse.us> wrote in message
news:K4******************@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 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.
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?


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


I suppose that should be "in an array initialization"

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

--
pete


--
pete
Nov 14 '05 #4

"pete" <pf*****@mindspring.com> wrote in message
news:41**********@mindspring.com...
pete wrote:

Stephen Mayes wrote:

"Stephen Mayes" <st*********@themayeshouse.us> wrote in message
news:K4******************@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 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.
>
>

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?


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


I suppose that should be "in an array initialization"


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

-Mike
Nov 14 '05 #5

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

Similar topics

19
by: Henry | last post by:
I finally thought I had an understanding of multi dimensional arrays in C when I get this: #include <stdio.h> #define max_x 3 #define max_y 5 int array;
20
by: Petter Reinholdtsen | last post by:
Is the code fragment 'char a = ("a");' valid ANSI C? The problematic part is '("a")'. I am sure 'char a = "a";' is valid ANSI C, but I am more unsure if it is allowed to place () around the...
3
by: Eric Laberge | last post by:
Aloha! I've been reading the standard (May '05 draft, actually) and stumbled across this: 6.7.1 Initialization §20 "If the aggregate or union contains elements or members that are aggregates...
18
by: Michael Press | last post by:
Hello. I am puzzled. A line of the form char array = { a}; or char array = { a, b, c}; is an array initializer.
30
by: questions? | last post by:
say I have a structure which have an array inside. e.g. struct random_struct{ char name; int month; } if the array is not intialized by me, in a sense after I allocated a
6
by: Kannan | last post by:
Hi, I have question about character array initialization. In section 6.7.8 paragraph number 21, it's given that "If there are fewer initializers in a brace-enclosed list than there are...
15
by: jamx | last post by:
How can you initialize an array, in the initialization list of a constructor ?? SomeClass { public: SomeClass() : *init here* { } private: int some_array; };
152
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { {...
24
by: DomoChan | last post by:
the code below will compile in visual c++ 2003, but im not sure its valid. unsigned char myString = ""; after this line executes, all the bytes within myString are indeed set to '0's' but is...
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
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.