Home AMX User Forum NetLinx Studio

Initializing a structure array error

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:
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]

Comments

  • Failing a quick visit to Google, a quick visit to wikipedia.org leads me to believe that this means "Right Hand Side" of an equation. I.E., initialization must by formatted as X = Y. Lacking an equal sign, there is no "RHS" to that assignment, so it fails.

    I don't believe you have any option aside from filling in the blanks in DEFINE_START.

    - Chip

    What's an RHS?
  • Thanks for the lookup of RHS 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
  • DHawthorneDHawthorne Posts: 4,584
    Don't put it in DEFINE_CONSTANT. Put it in DEFINE_VARIABLES with a CONSTANT keyword on the declaration.
  • AMXJeffAMXJeff Posts: 450
    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.
    DEFINE_TYPE
    
    STRUCTURE _ZoneInfoStruct
    {
     CHAR		cZoneName[38]
     INTEGER	nState
     INTEGER	nArrayPosition
     INTEGER	nPartitionNumber
    }
    
    DEFINE_VARIABLE
    
    VOLATILE _ZoneInfoStruct _sZoneInfo[128];
    
    DEFINE_START
    
    _sZoneInfo[1].cZoneName = 'Office Sliding Door';
    _sZoneInfo[1].nState = FALSE;
    _sZoneInfo[1].nArrayPosition = 1;
    _sZoneInfo[1].nPartitionNumber = 0;
    
    _sZoneInfo[2].cZoneName = 'Office Window';
    _sZoneInfo[2].nState = FALSE;
    _sZoneInfo[2].nArrayPosition = 2;
    _sZoneInfo[2].nPartitionNumber = 0;
    
    _sZoneInfo[3].cZoneName = 'Office Motion';
    _sZoneInfo[3].nState = FALSE;
    _sZoneInfo[3].nArrayPosition = 3;
    _sZoneInfo[3].nPartitionNumber = 0;
    
    
    

    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.
    VOLATILE _ZoneInfoStruct _sZoneInfo[128] = 
    {
    	{'Office Sliding Door', FALSE, 1, 0},
    	{'Office Window', FALSE, 2, 0},
    	{'Office Motion', FALSE, 3, 0}
    }
    


    Hope this helps!!!!
    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]
  • GSLogicGSLogic Posts: 562
    it's a little easier for entering the name values:
    DEFINE_CONTS
        cName[1] = 'Office Sliding Door';
        cName[2] = 'What ever 2';
        cName[3] = 'Some more 3;
        cName[...128];
    
    DEFINE_START
    for(i=1; 1<=128; i++)
    {
       _sZoneInfo[i].cZoneName = cName[i];
       _sZoneInfo[i].nState = FALSE;
       _sZoneInfo[i].nArrayPosition = 1;
       _sZoneInfo[i].nPartitionNumber = 0;
    }
    
  • 1) _sZoneInfo[128].cZoneName, is a single dimension array and you are treating it like it is a two dimension array.
    I'm not sure I understand... how am I treating it like a 2-dimensional array? _sZoneInfo[] is a single dimensional array, and .cZoneName is a single dimensional character array. In my attempt to initialize it, I'm initializing the cZoneName data in the single dimensional array of _sZoneInfo[].



    --John
  • AMXJeffAMXJeff Posts: 450
    I'm not sure I understand... how am I treating it like a 2-dimensional array? _sZoneInfo[] is a single dimensional array, and .cZoneName is a single dimensional character array. In my attempt to initialize it, I'm initializing the cZoneName data in the single dimensional array of _sZoneInfo[].
    --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...
  • AMXJeff wrote:
    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.

    _ZoneInfoStruct _sZoneInfo[128].cZoneName =
    {
    'Office Sliding Door', //Zone 1
    'Office Window', //Zone 2
    'Office Motion', //Zone 3

    ...more zones...
    }


    I hope this helps...
    Thanks AMXJeff. When you declare the data type in the DEFINE_CONSTANT section, I believe you can initialize it this way (except not for a structure). I always initialize my constant character arrays in this way. Here's an example:
    DEFINE_CONSTANT
    
      CHAR sZONE_NAMES_ARRAY[128][37] =	
      {
    	 'Office Sliding Door',					                //Zone 1
    	 'Office Window',							//Zone 2
    	 'Office Motion',							//Zone 3
    	 'Theater Entry Door (Left)',                                           //Zone 4
    	 'Theater Entry Door (Right)',						//Zone 5
    	 'Theater Lobby Motion',						//Zone 6
    	 'Game Room Passage Door',						//Zone 7
    	 'Game Room Window 1',							//Zone 8
    
              ...More zones to #128...
      }
    
    
    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
  • DHawthorne wrote:
    Don't put it in DEFINE_CONSTANT. Put it in DEFINE_VARIABLES with a CONSTANT keyword on the declaration.

    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:
    DEFINE_CONSTANT
    
      CHAR sZONE_NAMES_ARRAY[128][37] =	
      {
    	 'Office Sliding Door',					                //Zone 1
    	 'Office Window',							//Zone 2
    	 'Office Motion',							//Zone 3
    	 'Theater Entry Door (Left)',                                           //Zone 4
    	 'Theater Entry Door (Right)',						//Zone 5
    	 'Theater Lobby Motion',						//Zone 6
    	 'Game Room Passage Door',						//Zone 7
    	 'Game Room Window 1',							//Zone 8
    
              ...More zones to #128...
      }
    
    
    DEFINE_START
    
    	 //Transfer the sZONE_NAMES_ARRAY into the _sZoneInfo Structure
    	 FOR (nLOOP=1; nLOOP <= nZONE_COUNT; nLOOP++)
    	 {
    		_sZoneInfo[nLOOP].cZoneName		=	sZONE_NAMES_ARRAY[nLOOP]
    		_sZoneInfo[nLOOP].nPartitionNumber	=	nZONE_PARTITION_ARRAY[nLOOP]
    		
    	 }
    

    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])
  • OOPS! Sorry Jeff, I said the same thing you said. I misread your post :).

    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
Sign In or Register to comment.