Thread: Array can't be created (expression must have a constant value)

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171

    Question Array can't be created (expression must have a constant value)

    Hi! I've try to make a small program that shows the error, but I still don't understand why this doesn't work.

    If I have the code:
    Code:
    #include <stdio.h>
    
    
    int main() {
    
    
    	char genKey[10];
    
    
    	return 0;
    }
    It works fine. But if I try to create a variable and initialize it to 10. Then Visual Studio complains.

    Code:
    #include <stdio.h>
    
    
    int main() {
    
    
    	int textLen = 10;
    	char genKey[textLen];
    
    
    	return 0;
    }
    I thought this meant the same thing, but it doesn't? Because the code in the second example doesn't compile.

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,112
    You are using a compiler that still adheres to the C89 C Standard, by default. Variable size arrays are valid only using the C99 Standard or higher.

    It is possible that there is an option in your compiler to set the level to C99. In gcc, the option would be "-std=C99"

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You are using a compiler that still adheres to the C89 C Standard, by default.
    The Microsoft C compiler supports all of C90 any feature of the latest standards are only supported if they are required for C++ compliance. And since VLA are not supported by any of the current C++ standards they will probably not support this "feature" with their current compilers.

    When declaring arrays you should either use a named constant (#define ARRAY_SIZE 10) or constant values.

    By the way VLA are only truly supported by the C99 standard. Any standard before C99 this "feature" was not allowed, and with C11 the feature is optional and not required to be supported.

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    You can get the near equivalent of VLA using _alloca(). The difference is that a VLA will be freed when it goes out of scope (such as a local block created using left and right braces), while an _alloca() object will be freed when the function exits.

    Code:
    #include <stdlib.h>
    int main()
    {
        int textLen = 10;
        char * genKey = _alloca(textLen*sizeof(char));
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    Thanks. It helped

  6. #6
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    I am not sure if using alloca or _alloca is a good idea - see this discussion
    c - Why is the use of alloca() not considered good practice? - Stack Overflow

    If you don't like dynamic memory management it would be better to use C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character arrays - constant values in a constant expression
    By hewlettuser in forum C++ Programming
    Replies: 2
    Last Post: 04-07-2017, 04:00 AM
  2. expected constant expression?
    By GroogFish in forum C Programming
    Replies: 3
    Last Post: 05-08-2012, 12:17 AM
  3. illegal constant expression
    By nta in forum C Programming
    Replies: 3
    Last Post: 03-13-2008, 11:35 AM
  4. Does every expression results into constant value?
    By forumuser in forum C Programming
    Replies: 12
    Last Post: 04-18-2007, 08:08 AM

Tags for this Thread