Thread: Why const variable can not define array bounds.

  1. #1
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200

    Why const variable can not define array bounds.

    Hello,

    I found out that below code is illegal:

    Code:
    const int a = 10;
    int b[a];
    So, why second line is incorrect? After all content of a cannot be change since it is declared as 'const'. Second even if it wasn't const, they memory for array shall be reserved once, so who cares what happened to variable a (in this example) after memory for array was allocated?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MartinR
    So, why second line is incorrect?
    Basically just the rules of C concerning what exactly is a constant expression in this context, e.g., an integer constant like 10 is a constant expression, but an int variable like a, even if declared const, is not. Consequently, one way out is to use a macro to define the constant, since macro replacement would cause the identifier to be replaced by the integer constant during preprocessing.

    Quote Originally Posted by MartinR
    After all content of a cannot be change since it is declared as 'const'.
    True, but it can be more complicated, e.g.,
    Code:
    const int a = c;
    int b[a];
    If c is not const, then at run time it is possible for a to have varying values on each run (or each call of the enclosing function, etc). So disallowing this or saying that b is therefore a variable length array simplifies the situation. There are ways to deal with these more complex situations, of course, but standard C did not go down that path.

    Quote Originally Posted by MartinR
    Second even if it wasn't const, they memory for array shall be reserved once, so who cares what happened to variable a (in this example) after memory for array was allocated?
    Because the amount of memory to allocate for the array may be determined at compile time. What you have in mind is more of a variable length array, where the amount of memory to allocate may be determined at run time, but cannot change after being allocated. Therefore, it is necessary to determine the value at compile time, hence the rule about it being a constant expression.
    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

  3. #3
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200
    Got it! Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. const and #define
    By mohnish_khiani in forum C Programming
    Replies: 14
    Last Post: 02-27-2011, 02:29 AM
  2. const vs #define
    By boss_nova in forum C Programming
    Replies: 2
    Last Post: 07-02-2008, 01:57 PM
  3. #define vs const
    By @nthony in forum C Programming
    Replies: 12
    Last Post: 11-29-2006, 08:08 PM
  4. const vs. #define
    By Flarelocke in forum C++ Programming
    Replies: 5
    Last Post: 09-08-2001, 08:29 AM

Tags for this Thread