LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-14-2008, 06:11 PM   #1
vkmgeek
Member
 
Registered: Feb 2006
Location: Ahmedabad
Distribution: rhel5
Posts: 185
Blog Entries: 2

Rep: Reputation: 31
Array declaration in C


One silly question . . .
As far as I remember we can take only constant index of an array in C. In other words we can't take variable value as an index of array at the time of DECLARATION.

#include <stdio.h>

int main()
{
int i;
for(i=1; i < 10; i++)
{
char ch[i]; //Compile time error should come here
printf("\n Size of an array is: %d\n", sizeof(ch));
}
return 0;
}

And so this code should give Compile time Error... But it is not giving ... WHY ? Using GCC to compile
 
Old 05-14-2008, 06:40 PM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
And so this code should give Compile time Error... But it is not giving ... WHY ? Using GCC to compile
It is called a variable length array(VLA) and is part of the c99 standard. http://c0x.coding-guidelines.com/6.7.5.2.html
 
Old 05-15-2008, 10:38 AM   #3
vkmgeek
Member
 
Registered: Feb 2006
Location: Ahmedabad
Distribution: rhel5
Posts: 185

Original Poster
Blog Entries: 2

Rep: Reputation: 31
But so I have another question ...
This is only allowed when I am using GCC... is that correct ?

If I use some other compiler like VC compiler, this feature may not be available...
And in short, this is specific to "Compiler Implementation"... Is that right ?
 
Old 05-15-2008, 10:46 AM   #4
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Maybe you are having a bad day so I will just quote myself "...part of the c99 standard".
http://www.comeaucomputing.com/techt...lelengtharrays more info if you require it.

Last edited by dmail; 05-15-2008 at 10:48 AM.
 
Old 05-15-2008, 12:06 PM   #5
vkmgeek
Member
 
Registered: Feb 2006
Location: Ahmedabad
Distribution: rhel5
Posts: 185

Original Poster
Blog Entries: 2

Rep: Reputation: 31
Come on dmail...
You are making puzzles for me ...
I understand tht this is part of C99

But, is this still under implementation/design ?
Or every compiler should follow the c99 statndard ?
 
Old 05-15-2008, 12:18 PM   #6
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
In general not all compilers obey the same standards, or at least equally well. Even if a compiler "follows a certain standard", it may just mean it follows some parts of it and not all. Shortly put, if the code is ok by some standard, it will compile on compilers that follow that standard fully or by those parts that your code uses. With very new standards it could be that only parts of it are implemented in a compiler (say GCC compiler), and the rest come afterwards.

In short: use compilers that say they follow the standard as well as possible.

Last edited by b0uncer; 05-15-2008 at 12:19 PM. Reason: typos
 
Old 05-15-2008, 03:01 PM   #7
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Come on, vkmgeek...

DMail was just saying that you happened to be using a C99 feature on what happened to be a C99 conformant compiler (GCC). What's so puzzling about that ;-)?

Personally, I don't know how many other compilers out there are C99 conformant. Recent offerings from Sun and IBM - certainly. Microsoft? I have the new VS2008 - and frankly, I hardly even recognize standard C++ anywhere in all the .Net-interoperability-this and ATL-enhancements-that junk the IDE likes to throw in, much less what part of "C" is or isn't supported. My guess is that VS2008 has "token support" for C95, and not much more than that.

Your best bet is to look at the documentation for individual vendors to check:

a) Whether or not they claim conformance to any standard
... and, if so ...
b) which standards, at which levels

IMHO .. PSM

PS:
This is a good link:
http://en.wikipedia.org/wiki/C_(prog..._language)#C99
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Array declaration? kalleanka Programming 2 11-19-2007 06:27 AM
the c declaration kapsikum Programming 3 04-06-2005 02:12 AM
Array declaration in class or main function??? redhatrosh Programming 4 03-15-2005 02:13 PM
Is there a default size limit to a dictionary, list or array declaration ? Linh Programming 2 06-08-2004 11:35 AM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 02:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration