Does ansi c support variable-length array?


 
Thread Tools Search this Thread
Top Forums Programming Does ansi c support variable-length array?
# 1  
Old 09-09-2011
Does ansi c support variable-length array?

I successfully compiled code like below.

Code:
#include<stdio.h>
#include<string.h>


int main()
{
    int co = 9;
    char a[co];
    strcpy(a, "hahahah");
    printf("co=%d\n", co);
    printf("a=%s\n", a);
    return 0;
}

Code:
gcc com.c -o com -ansi -Wall -O0

# 2  
Old 09-09-2011
Yes. Variable Length Arrays is an ANSI C99 feature. But they are not supported under C++, neither under the previous C standard. Also, there is support for variable length arrays in many different compilers in the form of language extensions (like the GNU C extensions found in the GNU C compiler).
# 3  
Old 09-09-2011
It complaints a little with '-pedantic' option.
# 4  
Old 09-09-2011
Because '-pedantic' assumes that you're using ISO C90. In this previous standard, variable length array was forbidden.

If you want to use '-pedantic' , adds e.g. '-std=c99' to make sure you're checking against the ISO C99 standard.

/Lew
# 5  
Old 09-09-2011
Quote:
Originally Posted by vistastar
I successfully compiled code like below.

Code:
#include<stdio.h>
#include<string.h>


int main()
{
    int co = 9;
    char a[co];
    strcpy(a, "hahahah");
    printf("co=%d\n", co);
    printf("a=%s\n", a);
    return 0;
}

Code:
gcc com.c -o com -ansi -Wall -O0

If you would like the compilation to fail -- perhaps because you need the code to conform to an older version of the C standard -- then you'd need to specify the c dialect with -std, specify that you'd like to be -pedantic, and turn warnings into errors.

For example, to strictly conform to c90 (synonymously c89) and reject any attempt to use a feature that is not part of that standard (such as a variable length array), gcc can be forced to abort compilation with a non-zero exit status with the following options:
Code:
gcc -std=c90 -pedantic -Werror ...

Regards,
Alister

Last edited by alister; 09-09-2011 at 10:52 AM..
# 6  
Old 09-09-2011
I've had compilers not complain about variable-length arrays and still bungle them. Their dependence on them made the code quite difficult to repair, too. I wouldn't consider it a portable or reliable feature.
# 7  
Old 09-09-2011
You can use malloc to reliably create almost any size array dynamically. This has been the standard approach forever.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert variable length record to fixed length

Hi Team, I have an issue to split the file which is having special chracter(German Char) using awk command. I have a different length records in a file. I am separating the files based on the length using awk command. The command is working fine if the record is not having any... (7 Replies)
Discussion started by: Anthuvan
7 Replies

2. Shell Programming and Scripting

Array Length Reports as Having Length when it is Empty?

Hello All, I have this script that does stuff like "starting, stopping & restarting" a Daemon Process running on my machine... My main question is why in part of my code (which you will see below) does the Array Length (i.e. ${#PIDS} ) return "1" when I know the Array is empty..? Here is... (17 Replies)
Discussion started by: mrm5102
17 Replies

3. Programming

why the implementatoin of Bakery algorithm in ANSI C does not work in ANSI C

I follow the description of wiki (Lamport's bakery algorithm - Wikipedia, the free encyclopedia), then implement that algorithm in C, but it doesn't work, Starving is still here, is the implementation worry? Only print out: Thread ID: 0 START! Thread ID: 0 END! Thread ID: 0 START!... (2 Replies)
Discussion started by: sehang
2 Replies

4. Shell Programming and Scripting

MAWK does not support length(array)?

As Brendan O'Conner writes in this blog, mawk is near 8 times faster than gawk, so I am going to give mawk a go, but I got errors when trying to print the length of an array in mawk using length() function, is it not supported in mawk? or there's another way to get the length of an array in mawk? ... (3 Replies)
Discussion started by: kevintse
3 Replies

5. Shell Programming and Scripting

changing a variable length text to a fixed length

Hi, Can anyone help with a effective solution ? I need to change a variable length text field (between 1 - 18 characters) to a fixed length text of 18 characters with the unused portion, at the end, filled with spaces. The text field is actually field 10 of a .csv file however I could cut... (7 Replies)
Discussion started by: dc18
7 Replies

6. Shell Programming and Scripting

Variable Sized Array Length Question

I need to implement the following logic and need some expert help from UNIX community. These are the steps in my Shell script. 1. Analyze a file. 2. Extract all the ID's in that file. 3. Use the ID's from #2 to run another filter on the file. I've implemented # 1 and 2 using... (3 Replies)
Discussion started by: katwala
3 Replies

7. Shell Programming and Scripting

Make variable length record a fixed length

Very, very new to unix scripting and have a unique situation. I have a file of records that contain 3 records types: (H)eader Records (D)etail Records (T)railer Records The Detail records are 82 bytes in length which is perfect. The Header and Trailer records sometimes are 82 bytes in... (3 Replies)
Discussion started by: jclanc8
3 Replies

8. Red Hat

-ansi flag support for g++

I have been compiling a cpp program using following command: g++ -c -ansi a.cpp with g++ version 4.1.2. I want to know the significance of the -ansi flag and which other higher g++ version supports this flag by default Dhiraj (1 Reply)
Discussion started by: dhiraj kurhade
1 Replies

9. Shell Programming and Scripting

Convert file from Unix - ANSI to PC - ANSI

Hi, I am creating a file in Unix using a shell script. The file is getting created in the Unix - ANSI format. My requirement is to convert it to the PC - ANSI format. Can anyone tell me how to do this? Thanks, Sunil (0 Replies)
Discussion started by: ssmallya
0 Replies

10. Shell Programming and Scripting

creating a fixed length output from a variable length input

Is there a command that sets a variable length? I have a input of a variable length field but my output for that field needs to be set to 32 char. Is there such a command? I am on a sun box running ksh Thanks (2 Replies)
Discussion started by: r1500
2 Replies
Login or Register to Ask a Question