473,549 Members | 2,777 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9679

"Stephen Mayes" <st*********@th emayeshouse.us> wrote in message
news:K4******** **********@bign ews1.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*********@th emayeshouse.us> wrote in message
news:K4******** **********@bign ews1.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*********@th emayeshouse.us> wrote in message
news:K4******** **********@bign ews1.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*****@mindsp ring.com> wrote in message
news:41******** **@mindspring.c om...
pete wrote:

Stephen Mayes wrote:

"Stephen Mayes" <st*********@th emayeshouse.us> wrote in message
news:K4******** **********@bign ews1.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
4538
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
7065
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 string literal.
3
2534
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 or unions, these rules apply recursively to the subaggregates or contained unions. If the initializer of a subaggregate or contained union begins...
18
2467
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
3312
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
34764
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 elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the...
15
3354
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
9765
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 = { { 3.14 }, { 42.6 } }; f((double *)array, sizeof array / sizeof **array); return 0;
24
2155
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 this really valid c++ or c? where can I find out how this is implemented? Im concerned because I had a 3rd party library wrapper which was
0
7477
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7991
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7509
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6084
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5396
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5118
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3503
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1084
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
790
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.