C18: assigning multidimensional arrays/stuctures

Discussion in 'Programmer's Corner' started by 0xFF, Feb 26, 2008.

  1. 0xFF

    Thread Starter Member

    Feb 26, 2008
    12
    0
    Hello all;

    Can anyone help me with the assignment of multidimensional arrays/structures with the C18 compiler, under MPLAB? I have been reading and reading, and can find all kinds of examples for every compiler BUT C18. Every example I have tried produces errors. Here's some of what I have tried;

    Code ( (Unknown Language)):
    1.  
    2. typedef struct BILL
    3. {
    4.     char Country[3];
    5.     char Value[3];
    6.     char Type;
    7.     char Series;
    8.     char Compatibility;
    9.     char Version;
    10. }
    11.  
    12. struct BILL billTypes[20];
    13.  
    Code ( (Unknown Language)):
    1. billTypes[0] = {0x55, 0x53, 0x44, 0x30, 0x30, 0x30, 0x2A, 0x2A, 0x2A, 0x2A};  // doesn't work
    2.  
    3. billTypes[0] = {{0x55, 0x53, 0x44}, {0x30, 0x30, 0x30}, 0x2A, 0x2A, 0x2A, 0x2A};  // doesn't work
    4.  
    5. billTypes[0] = {'U', 'S', 'D', '0', '0', '0', '*', '*', '*', '*'};  // doesn't work
    6.  
    7. billTypes[0] = {{'U', 'S', 'D'}, {'0', '0', '0'}, '*', '*', '*', '*'};  // doesn't work
    I've tried all kinds of brackets and configurations (ie. arrangement of the brackets). What DOES work, is this;

    Code ( (Unknown Language)):
    1. billTypes[0].Country[0] = 'U';
    2. billTypes[0].Country[1] = 'S';
    3. billTypes[0].Country[2] = 'D';
    4. billTypes[0].Value[0] = '0';
    5. billTypes[0].Value[1] = '0';
    6. billTypes[0].Value[2] = '0';
    7. billTypes[0].Type = '*';
    8. billTypes[0].Series = '*';
    9. billTypes[0].Compatibility = '*';
    10. billTypes[0].Version = '*';
    Or even;


    Code ( (Unknown Language)):
    1. billTypes[0].Country[0] = 0x55;
    2. billTypes[0].Country[1] = 0x53;
    3. billTypes[0].Country[2] = 0x44;
    4. billTypes[0].Value[0] = 0x30;
    5. billTypes[0].Value[1] = 0x30;
    6. billTypes[0].Value[2] = 0x30;
    7. billTypes[0].Type = 0x2A;
    8. billTypes[0].Series = 0x2A;
    9. billTypes[0].Compatibility = 0x2A;
    10. billTypes[0].Version = 0x2A;
    Obviously, this is the looooong way around and takes up a lot of space after defining even 7 billTypes. There has to be an easier way, which is eluding me with the C18 compiler.

    Any help would be appreciated. Thanks!
     
  2. balisong

    Member

    Feb 26, 2008
    27
    0
    You have to the initialization on the same line as the declaration.

    Code ( (Unknown Language)):
    1. BILL billTypes[2] = {
    2.     {{0x55, 0x53, 0x44}, {0x30, 0x30, 0x30}, 0x2A, 0x2A, 0x2A, 0x2A},
    3.     {{'U', 'S', 'D'}, {'0', '0', '0'}, '*', '*', '*', '*'}
    4. };
    Initialization is always all-or-nothing.
     
  3. 0xFF

    Thread Starter Member

    Feb 26, 2008
    12
    0
    Hi balisong;

    Thanks for your reply.

    It seems odd to me that there is no way to assign an entire array after it has been initialized.

    The problem I have is the array is initialized to be global, but the assignment is not done until the bill table is read from the device.

    Anyway, I'll deal with the way it is for now, but may have to rethink the initialization of the array (and whether I really need it to be global). Well, thanks for helping me understand WHY it will not work! :)
     
Loading...