1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Declare & Assign an array out side of function!

Discussion in 'C' started by boomeral, Feb 2, 2008.

  1. boomeral

    boomeral New Member

    Joined:
    Feb 2, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hello Friends,

    This seems to be trivial but bugging me a lot. I am trying to create a header file with following statements:

    Char BinaryCMD[8] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x06};
    int temp calcCRC16(6, BinaryCMD);
    array[6] = temp >> 8;
    array[7] = temp;

    calcCRC16 calculates 16-bit CRC and takes number of bytes and a pointer to the array as its arguments.
    First two lines are ok in the code; however, 3rd and 4th lines cause compiler to fail. If I place this entire code in a function – say main() – it works ok. But anywhere outside a function, it fails. How can I solve this problem?


    Thanks
     
  2. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    Put it inside main.

    C has no concept of having executable code outside of functions. C++ can do it, but that's only for constructing global objects.
     
  3. boomeral

    boomeral New Member

    Joined:
    Feb 2, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Interesting enough it is a global object. Can you give me an example of c++ global object the way I am trying to do it?

    Thanks,
     
  4. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    Why is doing it once at the start of main such a big issue to you?
     
  5. boomeral

    boomeral New Member

    Joined:
    Feb 2, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    If it were not an issue, I would not have asked this question from the get to. There are about 1000 of these commands have to be declared - more or less. It would be nice to have all of them in once place. If modification or alteration is needed, main would not look as crowded.
     
  6. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    So create a function called 'initGlobalVariables' which is called right at the start of main.
     

Share This Page