C18: assigning multidimensional arrays/stuctures

Thread Starter


Joined Feb 26, 2008
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;

Rich (BB code):
typedef struct BILL
    char Country[3];
    char Value[3];
    char Type;
    char Series;
    char Compatibility;
    char Version;

struct BILL billTypes[20];
Rich (BB code):
billTypes[0] = {0x55, 0x53, 0x44, 0x30, 0x30, 0x30, 0x2A, 0x2A, 0x2A, 0x2A};  // doesn't work

billTypes[0] = {{0x55, 0x53, 0x44}, {0x30, 0x30, 0x30}, 0x2A, 0x2A, 0x2A, 0x2A};  // doesn't work

billTypes[0] = {'U', 'S', 'D', '0', '0', '0', '*', '*', '*', '*'};  // doesn't work

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;

Rich (BB code):
billTypes[0].Country[0] = 'U';
billTypes[0].Country[1] = 'S';
billTypes[0].Country[2] = 'D';
billTypes[0].Value[0] = '0';
billTypes[0].Value[1] = '0';
billTypes[0].Value[2] = '0';
billTypes[0].Type = '*';
billTypes[0].Series = '*';
billTypes[0].Compatibility = '*';
billTypes[0].Version = '*';
Or even;

Rich (BB code):
billTypes[0].Country[0] = 0x55;
billTypes[0].Country[1] = 0x53;
billTypes[0].Country[2] = 0x44;
billTypes[0].Value[0] = 0x30;
billTypes[0].Value[1] = 0x30;
billTypes[0].Value[2] = 0x30;
billTypes[0].Type = 0x2A;
billTypes[0].Series = 0x2A;
billTypes[0].Compatibility = 0x2A;
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!


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

Rich (BB code):
BILL billTypes[2] = {
    {{0x55, 0x53, 0x44}, {0x30, 0x30, 0x30}, 0x2A, 0x2A, 0x2A, 0x2A},
    {{'U', 'S', 'D'}, {'0', '0', '0'}, '*', '*', '*', '*'}
Initialization is always all-or-nothing.

Thread Starter


Joined Feb 26, 2008
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! :)