Thread: Expected Constant Expression in one compiler

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    3

    Expected Constant Expression in one compiler

    I declare count2 earlier in the program. In one compiler, this doesn't cause an error, but my compiler at school (Microsoft Visual C++) is telling me that a constant expression is required. Does anyone know how I could fix this?

    Code:
    lottery lotto_number[count2];

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Yes, declare count as a const. The index value of an array declared like that cannot be a variable.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The g++ compiler supports a non-const value for the array size because it is allowed in C even though it is not allowed in C++. That is why it works on one compiler even though it doesn't on another.

    You probably don't need to or want to use that g++ extension. Use vectors for dynamic arrays instead.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Just another one of those examples which show its better to learn the language rather than a compiler's implementation of that language.

    Otherwise, how would you ever be able to tell the difference between what's guaranteed by the standard (you expect it to work the same on all compilers) or some specific extension.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CreateWindow, primary expression expected
    By scwizzo in forum Windows Programming
    Replies: 3
    Last Post: 07-20-2008, 09:52 AM
  2. illegal constant expression
    By nta in forum C Programming
    Replies: 3
    Last Post: 03-13-2008, 11:35 AM
  3. expected constant expression
    By Brian in forum C Programming
    Replies: 4
    Last Post: 12-03-2003, 03:30 PM
  4. sizeof and Expression Evaluation
    By Dave_Sinkula in forum C Programming
    Replies: 2
    Last Post: 08-11-2003, 07:11 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM