illegal constant expression

This is a discussion on illegal constant expression within the C Programming forums, part of the General Programming Boards category; Code: My code is as follows: --------------------------------------------------------------------------- main.c --------------------------------------------------------------------------- long VALUE_IP_1,VALUE_IP_2,VALUE_IP_3,VALUE_IP_4; //global variables void main() { VALUE_IP_1 = (*(unsigned long ...

  1. #1
    nta
    nta is offline
    Registered User
    Join Date
    Mar 2008
    Posts
    1

    illegal constant expression

    Code:
    My code is as follows:
    ---------------------------------------------------------------------------
    main.c
    ---------------------------------------------------------------------------
    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.
    
    Thanks
    nta

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    make VALUE_IP_x defines and not the variables
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    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.
    Code:
    #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()
    {
        init_example_1();
        init_example_2();
    
        return 0;
    }
    gg

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, 04:45 PM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09: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 Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 02:53 PM

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