Question

I am trying to cut down my file lines by having one variable to insert into several arrays. So I'd like to have a string, or an array variable, such as the following:

new combomeals[] = { 
    3025101, 3025100, 3003000, 3025002, 3030101, 3025001, 3025000, 3021602, 3031402, 3020100, 
    3031401, 3031400, 3031302, 3020242, 3031301, 3031300, 3021702, 3021701, 3021700, 3020602, 
    3021601, 3030100, 3021600, 3021502, 3021501, 3020201, 3021500, 3020150, 3020502, 3020501, 
    3020500, 3001802, 3001801, 3001800, 3001702, 3001701, 3001700, 3011602, 3011601, 3011600, 
    3011502, 3020202, 3011501, 3011500, 3011232, 3011231, 3011202, 3011201, 3010602, 3010601, 
    3010600, 3010902, 3010901, 3010900, 3011102, 3011101, 3011100, 3011002, 3011001, 3011000, 
}

It's actually much longer than that, but I shortened it for the explanation... I want to take combomeals[] and put that into other arrays. The following does not work, but you will get the idea:

new first_array[] = { 
    1, 
    2, 
    3,
    combomeals[], 
}

new second_array[] = { 
    4, 
    5, 
    6,
    combomeals[], 
}

new third_array[] = { 
    7, 
    8, 
    9,
    combomeals[], 
}

The end goal is to put the first array into several other arrays. Please let me know if this makes sense and if you are able to help!

Thank you!

Was it helpful?

Solution

You cannot extend an array in the way you are attempting. Assuming that new has been aliased to an integral type, then the compiler will only accept the initializer list for first_array if all the elements in the list are the same type. But the last element of the list is a syntactic error, as combomeals is an array, and combomeals[] doesn't belong in an initializer list.

Similarly for the second_array and third_array.

You can accomplish something similar by putting the numbers in combomeals into a macro instead:

#define COMBOS \
3025101, 3025100, 3003000, 3025002, 3030101, 3025001, 3025000, 3021602, 3031402, 3020100, \
3031401, 3031400, 3031302, 3020242, 3031301, 3031300, 3021702, 3021701, 3021700, 3020602, \
3021601, 3030100, 3021600, 3021502, 3021501, 3020201, 3021500, 3020150, 3020502, 3020501, \
3020500, 3001802, 3001801, 3001800, 3001702, 3001701, 3001700, 3011602, 3011601, 3011600, \
3011502, 3020202, 3011501, 3011500, 3011232, 3011231, 3011202, 3011201, 3010602, 3010601, \
3010600, 3010902, 3010901, 3010900, 3011102, 3011101, 3011100, 3011002, 3011001, 3011000,

new combomeals[] = { COMBOS };

new first_array[] = { 1, 2, 3, COMBOS };
new second_array[] = { 4, 5, 6, COMBOS };
new third_array[] = { 7, 8, 9, COMBOS };

If your compiler has trouble dealing with a long source line, you may have to split COMBOS apart. It is less convenient, but a c89 compiler is only required to support a source file length 509 bytes long.

#define COMBOS1 \
3025101, 3025100, 3003000, 3025002, 3030101, 3025001, 3025000, 3021602, 3031402, 3020100, \
3031401, 3031400, 3031302, 3020242, 3031301, 3031300, 3021702, 3021701, 3021700, 3020602, \
3021601, 3030100, 3021600, 3021502, 3021501, 3020201, 3021500, 3020150, 3020502, 3020501
#define COMBOS2 \
3020500, 3001802, 3001801, 3001800, 3001702, 3001701, 3001700, 3011602, 3011601, 3011600, \
3011502, 3020202, 3011501, 3011500, 3011232, 3011231, 3011202, 3011201, 3010602, 3010601, \
3010600, 3010902, 3010901, 3010900, 3011102, 3011101, 3011100, 3011002, 3011001, 3011000

new combomeals[] = {
    COMBOS1,
    COMBOS2,
};

new first_array[] = {
    1, 2, 3,
    COMBOS1,
    COMBOS2,
};

/* ...etc... */

You may need to further break down the lines if your C compiler is non-conforming.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top