Thread: Const vs define and use of const

  1. #1
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68

    Const vs define and use of const

    Ok a silly question. I am pretty sure that is preferable to use const to declare constants rather than define which just replaces the text on your code with the corresponding label but I was wondering why in a header file of mine where I declare my const it doesnt let me use them for arrays in some structs i made

    i.e

    Code:
    const int BODY_SIZE = 251;			
    const int BUFFER_SIZE = 256;		
    const int LOGIN_NL = 16;			
    
    const int ERROR = -1;
    const int SUCCESS = 0;
    const int SEND = 1;
    const int READ = 2;
    char buf[BUFFER_SIZE];				
    
    typedef struct mail{
            char sender[LOGIN_NL];
            char receiver[LOGIN_NL];
            char text[BODY_SIZE];
            struct mail* next;
    }mail;
    the compiler says
    error: variably modified β...β at file scope

    Any help

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Don't put any variables in headers.
    Define them in a .cpp file and use extern in the header files.

  3. #3
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68
    What do you mean use extern in header files. I declare them in a cpp file and redeclare them in a header with the keyword extern in front of them

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Phoenix_Rebirth View Post
    I declare them in a cpp file and redeclare them in a header with the keyword extern in front of them
    That's how you do it, although you don't assign a value to them in the header:
    Code:
    // cpp:
    const int my_int = 0;
    // h:
    extern const int my_int;
    But if you are doing this, it wasn't clear in your original post.

  5. #5
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68
    still the same, i dont know. Maybe I need some other command for compiling them. So far I had three files:
    client.c
    functions_Client.h
    objects.h

    so I also make another file objects.c
    to compile them what do I write
    Before I wrote gcc -o client.out client.c and that was it. So do I need another parameter now that I have another c file

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You can't use const like that in a C program to declare the length of an array. It must be a #define or a literal constant like say arr[10]
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    ... or an enum.

    using a const int like that will only work in C++ that I know of, though maybe C99 as well I don't know.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed