Variables are accepted as the subscription when declare a array

  • Thread starter lovecreatesbeauty
  • Start date
L

lovecreatesbeauty

It is strange. Happen to get following the fragment compiled
successfully. Is there anything wrong with my compiler or my mind?

Please guide me to correct my understanding on it.

Sincerely,

lovecreatesbeauty


$ cat p113.c
int main(void)
{
int len = 100;
int a[len];
}
$ gcc --version
gcc (GCC) 4.1.0
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

$ gcc -std=c89 p113.c
$ gcc -std=c99 p113.c
$
 
K

Keith Thompson

lovecreatesbeauty said:
It is strange. Happen to get following the fragment compiled
successfully. Is there anything wrong with my compiler or my mind?

Please guide me to correct my understanding on it.

$ cat p113.c
int main(void)
{
int len = 100;
int a[len];
} [...]

$ gcc -std=c89 p113.c
$ gcc -std=c99 p113.c
$

gcc isn't a conforming compiler unless you ask it nicely to act as
one.

% gcc -std=c89 p113.c
% gcc -std=c89 -pedantic p113.c
p113.c: In function `main':
p113.c:4: warning: ISO C90 forbids variable-size array `a'
%
 
K

Kenneth Brody

Keith said:
lovecreatesbeauty said:
It is strange. Happen to get following the fragment compiled
successfully. Is there anything wrong with my compiler or my mind?

Please guide me to correct my understanding on it.

$ cat p113.c
int main(void)
{
int len = 100;
int a[len];
}
[...]
gcc isn't a conforming compiler unless you ask it nicely to act as
one.

% gcc -std=c89 p113.c
% gcc -std=c89 -pedantic p113.c
p113.c: In function `main':
p113.c:4: warning: ISO C90 forbids variable-size array `a'
[...]

Is it really non-conforming? Are variable-sized arrays "forbidden", or
is it not part of the standard, but allowable as an extension?

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
R

Robert Gamble

Kenneth said:
Keith said:
lovecreatesbeauty said:
It is strange. Happen to get following the fragment compiled
successfully. Is there anything wrong with my compiler or my mind?

Please guide me to correct my understanding on it.

$ cat p113.c
int main(void)
{
int len = 100;
int a[len];
}
[...]
gcc isn't a conforming compiler unless you ask it nicely to act as
one.

% gcc -std=c89 p113.c
% gcc -std=c89 -pedantic p113.c
p113.c: In function `main':
p113.c:4: warning: ISO C90 forbids variable-size array `a'
[...]

Is it really non-conforming? Are variable-sized arrays "forbidden", or
is it not part of the standard, but allowable as an extension?

c89 draft
3.5.4.2 Array Declarators:
Constraints:
The expression that specifies the size of an array shall be an integral
constant expression that has a value greater than zero.

The program is non-conforming. A conforming implementation must
provide a diagnostic but may then continue to do what ever it likes as
the behavior is no longer defined.

Robert Gamble
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top