Thread: Best way to declare an array size

  1. #1
    Registered User
    Join Date
    Jan 2018
    Posts
    3

    Best way to declare an array size

    Hello everyone,

    What is the best way to give a size to an array ?

    Either :

    Code:
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
    const int size = 4; char name[size]; return 0;
    }
    Or :

    Code:
    #include <stdio.h>
    #define SIZE 4
    
    int main(int argc, char **argv)
    {
    char name[SIZE]; return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The first way is C++ and does not work with most real C Compilers.

    I suggest getting a real C compiler to learn C programming.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Jan 2018
    Posts
    3
    Thanks stahta01,

    In my case I use the GCC compiler.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    GCC is a good compiler to use, at least in my opinion. It is important to realize though that GCC is a collection of compilers, which are built to be versatile, and be very forgiving with code. In order to properly use it you have to be careful to run the correct program with the correct options. I will say that the reason that this is a problem for C is because it was only briefly made into a feature in C99. The compiler would interpret something like name[size] as a variable length array. Then when C11 came out this feature was made optional again for compilers to implement.

    So it is best not to rely on VLAs if the code needs to be portable. At best, VLAs simplify what is offered by the malloc/realloc/free family of functions and do so in a way that is not as flexible. Once a VLA is declared, it will stay that length forever, whereas with realloc you can later adjust the size.

    Here is an example of the code being compiled in various ways.

    Code:
    C:\Users\jk\Desktop>gcc --version
    gcc (tdm64-1) 5.1.0
    Copyright (C) 2015 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.
    
    C:\Users\jk\Desktop>more foo.c
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
        const int size = 4;
        char name[size];
        return 0;
    }
    
    C:\Users\jk\Desktop>g++ -c foo.c
    
    C:\Users\jk\Desktop>del foo.o
    
    C:\Users\jk\Desktop>gcc -ansi -pedantic -c foo.c
    foo.c: In function 'main':
    foo.c:6:5: warning: ISO C90 forbids variable length array 'name' [-Wvla]
         char name[size];
         ^
    
    C:\Users\jk\Desktop>del foo.o
    
    C:\Users\jk\Desktop>gcc -std=c99 -pedantic -c foo.c
    
    C:\Users\jk\Desktop>del foo.o
    
    C:\Users\jk\Desktop>gcc -std=c11 -pedantic -c foo.c
    
    C:\Users\jk\Desktop>
    As I said, GCC is very forgiving. First, I compiled as C++, which went fine, and then I tried various standards. I do think that GCC is a real C compiler. After all, it caught the problem when compiling under C90 rules.

  5. #5
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    Code:
    const int size = 4;
    char name[size];
    Why is this a VLA when size is known at compile time ?

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Prior to VLAs, this was a limitation of the language for a different reason: const variables were not considered the same as constant expressions, and thus they didn't work for fixed size arrays. VLAs are simply a way for this kind of code to work, and nothing more. Sorry if it feels like I'm not answering the question...

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by whiteflags
    First, I compiled as C++, which went fine
    Quote Originally Posted by OldGuy2
    Why is this a VLA when size is known at compile time ?
    In C++ the array is not a variable length array since the declaration of size as a const int initialised with a integer constant expression allows it to be used as a integer constant expression, even though not all integer objects declared const (rather than constexpr) can be used in that way, e.g., if size had been initialised to argc, the declaration would then either be illegal, or would require a variable length array language extension (which g++ does provide). This is thus different from the rules in C, where size would not be regarded as a integer constant expression either way (as in whether initialised to 4 or to argc), hence the declaration in the original code would either be illegal or would result in a variable length array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    Thanks laserlight & whiteflags

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. declare functions with a variable's size
    By cable in forum C Programming
    Replies: 2
    Last Post: 02-01-2012, 03:43 PM
  2. How do you declare an array in a class?
    By casablanca in forum C++ Programming
    Replies: 9
    Last Post: 08-03-2011, 12:16 PM
  3. Replies: 9
    Last Post: 02-12-2011, 10:50 AM
  4. How To Declare and Dynamically Size a Global 2D Array?
    By groberts1980 in forum C Programming
    Replies: 26
    Last Post: 11-15-2006, 09:07 AM
  5. How to declare this array?
    By ooosawaddee3 in forum C++ Programming
    Replies: 2
    Last Post: 10-13-2002, 01:51 PM

Tags for this Thread