q for the C hackers

Aaron Hope Aaron.Hope at unh.edu
Tue Aug 19 12:56:34 EDT 2003


On Tue, 2003-08-19 at 08:46, Bob Bell wrote:

>     Actually, the following is valid C99:
>         const int m = 10;
>         int
>         foo(int n) {
>             char s[n];
>             char t[m];
>         ...
>         }

Yes, C99 supports variable sized arrays, but that's not always what you
want.  They cannot be statically allocated and cannot be initialized. 
Consider the following:

	const int m = 10;
	int buf[m];
	int
	foo(int n) {
	    char s[n] = { 0 };
	    char t[m];
	    // code
	}

% gcc -std=c99 t.c -o t
t.c:2: error: variable-size type declared outside of any function
t.c: In function `foo':
t.c:5: error: variable-sized object may not be initialized
t.c:5: warning: excess elements in array initializer
t.c:5: warning: (near initialization for `s')

aaron
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : http://mail.gnhlug.org/mailman/private/gnhlug-discuss/attachments/20030819/7fe78c57/attachment.bin


More information about the gnhlug-discuss mailing list