473,786 Members | 2,398 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Initialization of variable length arrays

Hi all.

The source code download bundle for "Beginning C: From Novice to
Professional, Fourth Edition" (ISBN: 1590597354) (Horton/Apress)
contains a C source file (program9_09.c) which contains several instances
of the following type of idiom:
/* Program 9.9 REVERSI An Othello type game */
const int SIZE = 6;
int main(void)
{
char board [SIZE][SIZE] = { 0 };
return 0;
}
Now I'm a complete newbie with C (hence me reading this book), but I've had
a glance at "ISO/IEC 9899:TC2" and as far as I can tell:

1) "6.7.5.2 Array declarators Point 4" tells me that this must be a
"variable length array", and

2) "6.7.8 Initialization Point 3" tells me that initializers do not
initialize variable length arrays.

Could the panel please tell me:

a) Am I on the right track here?
b) Is the above C illegal? Something very similar appears to
have been overlooked several times in the thread at:
http://groups.google.com.my/group/co...d4f798467dad19
c) Is the aforementioned book a known "lemon"? (Perhaps it's time for me
to try k&r2 again).

Thanks in advance to all. Jaime :-)
Jun 14 '07 #1
3 4911
On Jun 14, 4:35 pm, jaime <nospamfo...@ex ample.orgwrote:
Hi all.

The source code download bundle for "Beginning C: From Novice to
Professional, Fourth Edition" (ISBN: 1590597354) (Horton/Apress)
contains a C source file (program9_09.c) which contains several instances
of the following type of idiom:

/* Program 9.9 REVERSI An Othello type game */
const int SIZE = 6;
int main(void)
{
char board [SIZE][SIZE] = { 0 };
return 0;

}

Now I'm a complete newbie with C (hence me reading this book), but I've had
a glance at "ISO/IEC 9899:TC2" and as far as I can tell:

1) "6.7.5.2 Array declarators Point 4" tells me that this must be a
"variable length array", and

2) "6.7.8 Initialization Point 3" tells me that initializers do not
initialize variable length arrays.

Could the panel please tell me:

a) Am I on the right track here?
b) Is the above C illegal? Something very similar appears to
have been overlooked several times in the thread at:http://groups.google.com.my/group/co...ead/thread/246...
c) Is the aforementioned book a known "lemon"? (Perhaps it's time for me
to try k&r2 again).

Thanks in advance to all. Jaime :-)
You're right, it's broken. I think he used a C++ compiler, if he even
got it to compile.

$ gcc -Wall -ansi -pedantic -W -std=c99 broke.c
broke.c: In function 'main':
broke.c:5: error: variable-sized object may not be initialized
broke.c:5: warning: missing braces around initializer
broke.c:5: warning: (near initialization for 'board[0]')
broke.c:5: warning: excess elements in array initializer
broke.c:5: warning: (near initialization for 'board[0]')
broke.c:5: warning: excess elements in array initializer
broke.c:5: warning: (near initialization for 'board')
broke.c:5: warning: unused variable 'board'

$ g++ -W -Wall -ansi broke.c
broke.c: In function 'int main()':
broke.c:5: warning: unused variable 'board'

I have not read the book, but I guess it's a stinker.

Jun 14 '07 #2
jaime wrote:
Hi all.

The source code download bundle for "Beginning C: From Novice to
Professional, Fourth Edition" (ISBN: 1590597354) (Horton/Apress)
contains a C source file (program9_09.c) which contains several instances
of the following type of idiom:
/* Program 9.9 REVERSI An Othello type game */
const int SIZE = 6;
int main(void)
{
char board [SIZE][SIZE] = { 0 };
return 0;
}
This is legal C++, but not C99.

C++ made the sensible change to use 'const int' as a compile time
constant, so to a C++ compiler the array in question is not a VLA, but a
compile time fixed size array.

--
Ian Collins.
Jun 15 '07 #3
In article <f4***********@ energise.enta.n et>,
jaime <no*********@ex ample.orgwrote:
>const int SIZE = 6;
int main(void)
{
char board [SIZE][SIZE] = { 0 };
return 0;
}
>Now I'm a complete newbie with C (hence me reading this book), but I've had
a glance at "ISO/IEC 9899:TC2" and as far as I can tell:

1) "6.7.5.2 Array declarators Point 4" tells me that this must be a
"variable length array", and

2) "6.7.8 Initialization Point 3" tells me that initializers do not
initialize variable length arrays.
You're right as far as I can see, and gcc agrees. It would of course
work with #define SIZE 6. The program also tries to pass an array of
int to a function expecting an array of bool.

I suspect the author attempted to update it to take advantage of C99,
but failed to try compiling it (a mistake when posting to Usenet, and
a Really Big mistake when publishing a book).

(I see it also fails to check the return value from scanf(), resulting
in stupid behaviour if type in, say, "d2" instead of "2d".)

-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jun 15 '07 #4

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

Similar topics

27
2634
by: Mike P | last post by:
I will be passing my function a two dimensional array of varying length. Within that array is one data point, and the number of times it should loop through. So, for example, I might pass this to the function: example = new Array("A",2); example = new Array("Q",4); function loopIt(example);
5
14115
by: dam_fool_2003 | last post by:
Hai, I studied that the array size is fixed. But I come across a word called "variable length array". Is it possible to change the array size? So I tried the following: #include<stdio.h> #include<stdlib.h> int main(void) { int y = { 7, 9,10},i; for (;i<20;i++)
6
2070
by: JNY | last post by:
Hello, Is it possible to declare an array with variable indeces? i.e. int x = 4; int myArray; for (j = 0;j < 5;j++) {
23
19204
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
16
10533
by: John Kelsey | last post by:
Back in the "old" days with C, I used to do something like... struct { char Description; float price; } Items = { {"Apple", 1.99}, {"Banana", 2.04}
10
7401
by: Avalon1178 | last post by:
During a review of one of my peer's code, I ran into a section of code that was a bit suprising to me. Its got to do with initialization of a variable-length array. I ran a small application to convince myself whether it works or not, and it does work---however I couldn't explain it to myself why, so I thought I'd ask here. The sample code is below: char test = "hello world\n"; // test string, but I could easily have retrieved this...
2
8373
by: Antti Karanta | last post by:
Hi! Is it possible to inline initialize a struct whose one member is a string array of arbitrary length (terminated w/ a NULL ptr)? What I mean is something like this: typedef struct { char** x ; int y ;
10
5072
by: Juha Nieminen | last post by:
I suppose you can never know C++ fully. I would have never guessed this actually compiles and works: struct Point { int x, y; }; struct Line { Point endpoint; int weight; };
7
2799
by: Cromulent | last post by:
In section 6.7.5.2 it states the following: If the size is not present, the array type is an incomplete type. If the size is*instead of being an expression, the array type is a variable length array type of unspecified size, which can only be used in declarations with function prototype scope;124) such arrays are nonetheless complete types. If the size is an integer constant expression and the element
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9496
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9961
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4066
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.