Initializing a Structure
sonny
Posts: 208
Hello...
On the following block of example code I get a compile error...Too many elements in initializer.
........................................................ <-not in the code
DEFINE_TYPE
STRUCT testStruct
{
INTEGER number1
INTEGER number2
}
DEFINE_VARIABLE
testStruct oneTester = {2, 3}
testStruct anotherTester[] =
{
{5, 6},
{5, 6},
{5, 6},
{5, 6},
{5, 6},
{5, 6}
}
..................
The code will compile with only two elements in the array, but 3 or more yeilds this error. I can init many elements in a DEVCHAN array, but either there is a limitation or I have entered something incorrectly. Any ideas?
Thanks
On the following block of example code I get a compile error...Too many elements in initializer.
........................................................ <-not in the code
DEFINE_TYPE
STRUCT testStruct
{
INTEGER number1
INTEGER number2
}
DEFINE_VARIABLE
testStruct oneTester = {2, 3}
testStruct anotherTester[] =
{
{5, 6},
{5, 6},
{5, 6},
{5, 6},
{5, 6},
{5, 6}
}
..................
The code will compile with only two elements in the array, but 3 or more yeilds this error. I can init many elements in a DEVCHAN array, but either there is a limitation or I have entered something incorrectly. Any ideas?
Thanks
0
Comments
I think if you did teststruct[]={oneTester,oneTester,oneTester}
That would work
or if you did teststruct[4]
and then
teststruct[1] = {1,2}
teststruct[2] = {2,3}
etc etc
Regards, Leon
Or am I stuck with assigning each component in turn?
I'd like to do:
CD = {'Boston', 'Don't Look Back'}
instead of
CD.Artist = 'Boston'
CD.Title = 'Don't Look Back'
DEFINE_STRUCTURE _sMyStruct
{
CHAR First[20]
CHAR Last[20]
CHAR City[20]
}
DEFINE_VARIABLE
_sMyStruct NameList[20]
INTEGER X
DEFINE_FUNCTION FILL_MY_STRUCTURE(_sMyStruct MyStruct, INTEGER Row, CHAR FirstName[20],CHAR LastName[20],CHAR CityName[20])
{
MyStruct[Row].First = FirstName
MyStruct[Row].Last = LastName
MyStructRow].City = CityName
}
DEFINE_START
FOR(X=1.X<=LENGTH_ARRAY(NameList),X++)
{
SWITCH(X)
{
CASE 1: { FILL_MY_STRUCTURE (NameList,X,'Marc','Scheibein','Uhingen') }
CASE 2: { FILL_MY_STRUCTURE (NameList,X,'Any','Other','Items') }
}
}
(Code was written without compile verification, so use it "as is" and on your own risk!)
AMX is aware of the problem, but I don't think it's a big issue for them.
Unless I'm missing something that you're trying to say. You can initialize structures anywhere the code. I do it in BUTTON_EVENTS, DATA_EVENTS AND FUNCTIONS.
//EXAMPLE
DATA_EVENT[MASTER]
{
ONLINE:
{
//SET THE STRUCTURE TO DEFAULT
STACK_VAR X
IF(sTEST[1].nNUM = 0)
{
FOR(X =1; X <=5; X++)
sTEST[X].nNUM = X
}
}
}
You can even you multidimensional arrays in structures (even though it is not blessed by AMX)
STRUCTURE sTEMP
{
INTEGER nTEST[3][5][3]
}