Initializing a structure array error
John Gonzales
Posts: 609
Hi everyone,
I'm trying to initialize a structure array under DEFINE_CONSTANT. The problem is the structure isn't defined until later in the program under DEFINE_TYPE. Is there a simple way of initializing the array under DEFINE_CONSTANT? The other solution I can think of is to make a regular character array, then define the structure, then fill the structure array using a For loop under DEFINE_START.
When I do initialize the structure array under DEFINE_CONSTANT, I get the following error message: ERROR: (170): C10218: Constant array [_SZONEINFO] must be initialized with an RHS
What's an RHS?
Here's the code:
Thanks!
--John
[edited typos]
I'm trying to initialize a structure array under DEFINE_CONSTANT. The problem is the structure isn't defined until later in the program under DEFINE_TYPE. Is there a simple way of initializing the array under DEFINE_CONSTANT? The other solution I can think of is to make a regular character array, then define the structure, then fill the structure array using a For loop under DEFINE_START.
When I do initialize the structure array under DEFINE_CONSTANT, I get the following error message: ERROR: (170): C10218: Constant array [_SZONEINFO] must be initialized with an RHS
What's an RHS?
Here's the code:
DEFINE_CONSTANT _ZoneInfoStruct _sZoneInfo[128].cZoneName = { 'Office Sliding Door', //Zone 1 'Office Window', //Zone 2 'Office Motion', //Zone 3 ...more zones... } DEFINE_TYPE STRUCTURE _ZoneInfoStruct { CHAR cZoneName[38] INTEGER nState INTEGER nArrayPosition INTEGER nPartitionNumber } DEFINE_VARIABLE
Thanks!
--John
[edited typos]
0
Comments
I don't believe you have any option aside from filling in the blanks in DEFINE_START.
- Chip
I typo'd in the original post, so I corrected the typo's with an edit. The equal sign is in the actual code and still it wasn't working. I'm initializing the array a different way for now under Define_Start.
Thanks again.
--John
I see a couple issues...
1) _sZoneInfo[128].cZoneName, is a single dimension array and you are treating it like it is a two dimension array.
2) I have never seen Intializing a structure this way work ever 100%. I would make an object variable and intitialize it during startup of the master since it evident you are not going to change the information.
3) The intialization method you are using is only designed to work during the object decleration process, with that in mind you are trying to intialize a single member of the object instead of entire object. This is the way the book describes to do it, but again it has never worked and this does not compile either, so use the example above.
Hope this helps!!!!
--John
this is a two demension char array, this is the correct way to declare the variable and assign value to the first three index of the first dimension of the array. The second dimension is used to actually assign the values of the strings.
volatile char test[10][100] =
{
'Office Sliding Door', //Zone 1
'Office Window', //Zone 2
'Office Motion' //Zone 3
}
This is your decleration and assigment for your strucure object. FIrst thing is your can not access a member variable (cZoneName ) like this in the decleration statement. Second thing, if you could access cZoneName during decleration, the equal symbol is after the member variable, So your basically trying to assign three string values to a cZoneName at structure index 128. But like I said this decleration is totally invalid, so it will never work.
_ZoneInfoStruct _sZoneInfo[128].cZoneName =
{
'Office Sliding Door', //Zone 1
'Office Window', //Zone 2
'Office Motion', //Zone 3
...more zones...
}
This decleration is valid;
_ZoneInfoStruct _sZoneInfo[128]
// once you have a valid decleration you can access the members variables;
DEFINE_START
sZoneInfo[1].cZoneName = 'Office Sliding Door'
sZoneInfo[2].cZoneName = 'Office Window'
sZoneInfo[3].cZoneName = 'Office Motion'
I hope this helps...
In this case, I'm defining the size of the sZONE_NAMES_ARRAY to be a two dimensional array of 128 members with up to 37 characters per member and I'm initializing the data set at the same time.
This is different than when you're using it in other areas like DEFINE_START or DEFINE_EVENTS in which case you're correct that the number in the bracket would refer to the member's position as opposed to the array's size definition.
--John
Good advice. The answer to my question is that structures just can't be used in the DEFINE_CONSTANT section. The drawback to putting it in the DEFINE_VARIABLE section is that I like to have all of my constants in one section just to keep the code pretty and easy to modify.
The method I used to initialize it is a little more like what Gary and AMXJeff were mentioning.
Here's the code I used:
I did this when I couldn't define the structure array in the DEFINE_CONSTANT section, and it's working fine. It could have been done a little more efficiently using Dave's method, but this makes it easier in my opinion to change the zone name definitions since all of my other constant definitions are in the same place.
--John
(EDIT: changed sZONE_PARTITION_ARRAY[nLOOP] to nZONE_PARTITION_ARRAY[nLOOP])
I agree with you about the 2-dimensional constant array initialization, and as far as the structure initialization, again, it just can't be in the define constant section so you're right, it can't be initialized the way I tried to do it in my first post
--John