Which array is correct syntax in C

This is a discussion on Which array is correct syntax in C within the C Programming forums, part of the General Programming Boards category; Code: typedef enum {HOT, COLD} Temperature; int main() { Temperature TEMP; float A[TEMP] = {75, 12}; /* this work, compiled ...

  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,796
    >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,386
    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, 02:17 PM
  2. Syntax for constant array of array pointers
    By BMintern in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 09: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, 08: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21