Thread: illegal constant expression

  1. #1
    Registered User
    Join Date
    Mar 2008

    illegal constant expression

    My code is as follows:
    long VALUE_IP_1,VALUE_IP_2,VALUE_IP_3,VALUE_IP_4; //global variables
    void main()
    VALUE_IP_1 = (*(unsigned long *)0x44020000);
    VALUE_IP_2 = (*(unsigned long *)(0x44020000+4));
    VALUE_IP_3 = (*(unsigned long *)(0x44020000+8));
    VALUE_IP_4 = (*(unsigned long *)(0x44020000+12));
    I then created a diferent  file b.c
    extern long VALUE_IP_1,VALUE_IP_2,VALUE_IP_3,VALUE_IP_4;
    long var[4] = {VALUE_IP_1,VALUE_IP_2,VALUE_IP_3,VALUE_IP_4};
    When I compile the code I get following error.
    Error: illegal constant expression
    Please help me solve this problem.I am trying to read the value from the memory locations and store those values in array "var" everytime the program starts.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    make VALUE_IP_x defines and not the variables
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Join Date
    Oct 2007
    Inside my computer
    And don't use void main; use int main.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    If you must have a global array, then you will get this error even if you use #define's. You just have to initialize the array elements yourself.
    #define START_ADDR 0x44020000
    typedef unsigned long u_long;
    #define VALUE_IP_1 (*((volatile u_long*)START_ADDR)) 
    #define VALUE_IP_2 (*((volatile u_long*)(START_ADDR + 4)))
    #define VALUE_IP_3 (*((volatile u_long*)(START_ADDR + 8)))
    #define VALUE_IP_4 (*((volatile u_long*)(START_ADDR + 12)))
    u_long var[4];
    void init_example_1()
        var[0] = VALUE_IP_1;
        var[1] = VALUE_IP_2;
        var[2] = VALUE_IP_3;
        var[3] = VALUE_IP_4;
    void init_example_2()
        volatile u_long *src = (volatile u_long*)START_ADDR,
                        *src_end = src + (sizeof(var)/sizeof(*var));
        u_long *dest = var;
        for (; src < src_end; ++src, ++dest)
            *dest = *src;
    int main()
        return 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM