Thread: Which array is correct syntax in C

  1. #1
    DiepVien_007
    Guest

    Which array is correct syntax in C

    Code:
    typedef enum {HOT, COLD} Temperature;
    
    int main()
    {
      Temperature TEMP;
                              
      float A[TEMP] = {75, 12};  /* this work, compiled no error */        
      float B[Temperature] = {80, 20}; /*this also work too, no error, why?
    
    }
    I really can not figure out which array is correct syntax and legal, A or B?
    Can any one explain this to me.

    Thank you.

    DiepVien_007

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I really can not figure out which array is correct syntax and legal, A or B?
    Neither. A uses an automatic variable as the array size, a constant expression is required. B uses an enum type as the array size, which isn't legal as an expression. If your compiler accepts it then you're probably using special extensions that are on by default.

    -Prelude
    My best code is written with the delete key.

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    The size of the array should be a constant. Define a constant and use that as size of the array or just use a number.

    Code:
    #define CONSTANT 2
    
    int A [CONSTANT];
    int B [2];

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  2. Syntax for constant array of array pointers
    By BMintern in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 08:21 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM