Anytime Help Center

AMX How to Initialize Character Strings in the DEFINE_CONSTANT and DEFINE_VARIABLE Sections

Rating

Explanation:

In Axcess, initializing strings in the DEFINE_CONSTANT section was easy - you just wrote them the same way you would elsewhere in the program.

This doesn't work in NetLinx - there are some different rules to follow.

  • The strings must be enclosed by curly braces {}, not double-quotes " ".
  • Commas must separate all characters, unless it is only ASCII printable characters, AKA a string literal.
  • If it is a string literal, there are ways to initialize the string without separating every byte. Braces are optional.

However, there are some added benefits that are not possible in Axcess:

  • You can initialize the strings in either the DEFINE_CONSTANT or DEFINE_VARIABLE sections.
  • The compiler will determine the lengths for you - you don't need to explicitly give a length, just the [] to tell the compiler it's a string array.
  • You can initialize multidimensional arrays. The "total length" of the last dimension will be length of the longest string used to initialize the array.
  • If you are initializing a multidimensional array of string literals, if you specify the size of the last dimension, then you do not have to separate every byte with a comma.
  • In all other cases, specifying the size of the array is optional. Some programmers prefer to always specify the size when declaring or initializing arrays.

Examples:

A non-ASCII string:

sMyMIDIString[] = {$B0,7,127}

An ASCII string:

sMyString[] = {'H','e','l','l','o'} //This is the same syntax as used with non-ASCII strings or string expressions.

Or

sMyString[] = 'Hello' //This only works with ASCII string literals, it is the most similar to Axcess syntax.

Or

sMyString[] = {'Hello'} //Optionally, the ASCII string can be enclosed in braces.

A string expression:

sProjOn[] = {$02,'P','O','N',$03}

Notice that a string literal inside a string expression must have every byte separated by a comma.

A 2-dim array of strings:

sZoneNames[][] = {

{'M','a','s','t','e','r',' ','C','l','o','s','e','t'}, //Stat 1

{'M','a','s','t','e','r',' ','B','a','t','h'}, //Stat 2

{'M','a','s','t','e','r',' ','B','e','d','r','o','o','m'}, //Stat 3

{'M','a','s','t','e','r',' ','D','r','e','s','s','i','n','g'}, //Stat 4

 

{'M','a','s','t','e','r',' ','S','i','t','t','i','n','g'}, //Stat 5

{'K','i','t','c','h','e','n','/','U','t','i','l','i','t','y'},//Stat 6

{'T','h','e','a','t','e','r'}, //Stat 7

{'B','r','e','a','k','f','a','s','t'}, //Stat 8

 

{'G','a','m','e',' ','R','o','o','m'}, //Stat 9

{'O','f','f','i','c','e'}, //Stat 10

{'F','o','y','e','r'}, //Stat 11

{'G','r','e','a','t',' ','R','o','o','m'}, //Stat 12

 

{'D','i','n','i','n','g',' ','R','o','o','m'}, //Stat 13

{'G','u','e','s','t',' ','B','e','d','r','o','o','m'}, //Stat 14

{'T','V','/','S','t','u','d','y'}, //Stat 15

{'B','e','d','r','o','o','m',' ','4'}, //Stat 16

 

{'B','e','d','r','o','o','m',' ','3'}, //Stat 17

{'S','t','o','r','a','g','e'} //Stat 18

}

Alternate 2-dim array init for string literals ONLY:

You must specify the size of the last dimension as a length large enough to hold the longest string literal to be stored.

sZoneNames1[][16] =

{

'Master Closet', //Stat 1

'Master Bath', //Stat 2

'Master Bedroom', //Stat 3

'Master Dressing', //Stat 4

 

'Master Sitting', //Stat 5

'Kitchen/Utility', //Stat 6

'Theater', //Stat 7

'Breakfast', //Stat 8

 

'Game Room', //Stat 9

'Office', //Stat 10

'Foyer', //Stat 11

'Great Room', //Stat 12

 

'Dining Room', //Stat 13

'Guest Bedroom', //Stat 14

'TV/Study', //Stat 15

'Bedroom 4', //Stat 16

 

'Bedroom 3', //Stat 17

'Storage' //Stat 18

}


Downloads

Product

 

Topic

Programming

Related Articles

Last modified at 3/30/2023 12:05 PM by PRO Knowledge Base
Top