[comp.lang.c] Declaring array. Is this ok ?

aps@tut.fi (Suntioinen Ari) (11/12/90)

I was wondering if following is legal in (ANSI) C:

int somefunc(int i)
{
	int  array[i]; /* Can I use i here ? */
 ...
}


or


int i;

int somefunc(void)
{
	int  array[i]; /* How about here ? */
 ...
}

I tried to figure it out from K&R. It went clearly
over my understanding. 

Gcc seems to accept both.

Thanks for advance,

--
 +------------------------------+--------------------------------------------+
 ! Ari Suntioinen -- aps@tut.fi ! There is no dark side in the Force really. !
 !                              !     As a matter of fact it's all dark.     !
 +------------------------------+--------------------------------------------+

gwyn@smoke.brl.mil (Doug Gwyn) (11/12/90)

In article <APS.90Nov12074527@kaarne.tut.fi> aps@tut.fi (Suntioinen Ari) writes:
>	int  array[i]; /* Can I use i here ? */

No, array bounds must be integral constant expressions greater than zero.

henry@zoo.toronto.edu (Henry Spencer) (11/14/90)

In article <APS.90Nov12074527@kaarne.tut.fi> aps@tut.fi (Suntioinen Ari) writes:
>I was wondering if following is legal in (ANSI) C:
>int somefunc(int i)
>{
>	int  array[i]; /* Can I use i here ? */
> ...
>int i;
>...
>{
>	int  array[i]; /* How about here ? */

No.  Array dimensions must be constant expressions, known at compile time.

>Gcc seems to accept both.

Gcc compiles an interesting language which is not ANSI C.
-- 
"I don't *want* to be normal!"         | Henry Spencer at U of Toronto Zoology
"Not to worry."                        |  henry@zoo.toronto.edu   utzoo!henry

karl@ima.isc.com (Karl Heuer) (11/14/90)

In article <APS.90Nov12074527@kaarne.tut.fi> aps@tut.fi (Suntioinen Ari) writes:
>Gcc seems to accept both.

The default configuration of gcc includes several extensions over ANSI C.
Use "gcc -ansi -pedantic" to test this sort of thing.
	$ cat try.c
	int foo(int n) { int a[n]; }
	$ gcc -c try.c
	$ gcc -ansi -pedantic -c try.c
	try.c: In function foo:
	try.c:1: warning: ANSI C forbids variable-size array `a'
	$ exit

Karl W. Z. Heuer (karl@ima.isc.com or uunet!ima!karl), The Walking Lint

ronald@robobar.co.uk (Ronald S H Khoo) (11/14/90)

>  == henry@zoo.toronto.edu (Henry Spencer)
>> == aps@tut.fi (Suntioinen Ari)

>>I was wondering if following is legal in (ANSI) C:
>>int somefunc(int i)
>>{
>>	int  array[i]; /* Can I use i here ? */

>  No.  Array dimensions must be constant expressions, known at compile time.

>> Gcc seems to accept both.

> Gcc compiles an interesting language which is not ANSI C.

gcc compiles several different languages depending on what switches
you give it, viz:

	$ cat foo.c
	void foo(int i)
	{
		char bar[i];
	}
	$ gcc -c foo.c
	$ gcc -ansi -pedantic -c foo.c
	foo.c: In function foo:
	foo.c:3: warning: ANSI C forbids variable-size array `bar'
	$ 

Sure, it continues to produce a .o file, but isn't the diagnostic issued
above adequate to satisfy ANSI ?

As a matter of interest, is anyone keeping a publicly available list of
gcc's nonconformances ?  There seems to be a lot of "but gcc isn't conformant"
going round with little substantive data to back up that statement.
Sure, gcc currently doesn't have its own conforming library, but I don't
think that's the question being mooted here.

Moral: if you don't use the right compiler switches, you can even lead
prophets astray :-)
-- 
ronald@robobar.co.uk +44 81 991 1142 (O) +44 71 229 7741 (H)

tanner@cdis-1.compu.com (Dr. T. Andrews) (11/16/90)

hs-) Gcc compiles an interesting language which is not ANSI C.
True normally, but you can disable the extensions by command
line flags.
-- 
Section 801. Relation to the charter government.  All those functions
now performed by the Board of County Commissioners, the tax assessor
and tax collector for the district school board after the effective
date of this charter, shall be performed by the appropriate
department or division of the charter government.  (Sp. Acts, Ch.
70-966, Art. VIII)
 **
uflorida!ki4pv!cdis-1!tanner {uunet dsinc}!cdin-1!cdis-1!tanner

lerman@stpstn.UUCP (Ken Lerman) (11/16/90)

In article <1990Nov14.091809.10991@robobar.co.uk> ronald@robobar.co.uk (Ronald S H Khoo) writes:
[...]
>Moral: if you don't use the right compiler switches, you can even lead
>prophets astray :-)
    ^
    |  
    +------------+
                 |
                 ^ 
Or even worse, profits  :-) :-)
>-- 
>ronald@robobar.co.uk +44 81 991 1142 (O) +44 71 229 7741 (H)


Ken