Home NetLinx Studio

Initializing a Structure

sonnysonny Junior MemberPosts: 208

On the following block of example code I get a compile error...Too many elements in initializer.

........................................................ <-not in the code

STRUCT testStruct
INTEGER number1
INTEGER number2


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?



  • alexanboalexanbo Junior Member Posts: 282
    I don't think you can use an array to initialize a structure like that.

    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
  • Structure variables can only be initialized in the DEFINE_START

    Regards, Leon
  • youstrayoustra Junior Member Posts: 135
    Is there any streamlined way to assign values to a structure (like can be done w/ arrays)?

    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'
  • Marc ScheibeinMarc Scheibein Junior Member Posts: 669
    The idea what I have is to write a small function, and to use it in a Loop in DEFINE_START.

    CHAR First[20]
    CHAR Last[20]
    CHAR City[20]

    _sMyStruct NameList[20]

    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

    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!)
  • As Leon says, structure variables can only be initialized in DEFINE_START. This is a problem in the NetLinx compiler. You can initialize a built-in structure (ie DEVCHAN) when it's declared, but that doesn't work for user-defined data types. This makes for a problem if you need to make a structure variable CONSTANT (CONSTANT variables must be initialized when they're declared.)
    AMX is aware of the problem, but I don't think it's a big issue for them.
  • GSLogicGSLogic Original Member Posts: 562
    Structure variables can only be initialized in the DEFINE_START

    Regards, Leon

    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.

    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)
    INTEGER nTEST[3][5][3]
  • What I was trying to say, is that you can not initialize a structure arry in the define variable section. And the define start is a very locical place to put in the initialisation. Of course, this could be a function. And filling the structure could every where in events, functions or in define program. But it is not possible to do initialisation in the define variable section.
  • GSLogicGSLogic Original Member Posts: 562
    That's what I thought you were saying, but I just wanted to clarifie it so Sonny wouldn't get confused.
  • DHawthorneDHawthorne Old Timer Posts: 4,584
    The problem is how memory is allocated and in what order. Structures aren't allocated until after the DEFINE_VARIABLE items are; they can't be, actually, since their elements may depend on those other definitions.
Sign In or Register to comment.