Home AMX User Forum NetLinx Studio
Options

Array of Strings

Is it possible to do an array with strings (or characters) in it?

I'd like to reference this by array index
cINPUTNAMES[] = {'INPUT #1', 'INPUT #2', 'INPUT #3', 'INPUT #4', 'INPUT #5', 'INPUT #6', 'INPUT #7', 'INPUT #8', 'INPUT #9', 'INPUT #10', 'INPUT #11', 'INPUT #12', 'INPUT #13', 'INPUT #14', 'INPUT #15', 'INPUT #16', 'INPUT #17', 'INPUT #18', 'INPUT #19', 'INPUT #20', 'INPUT #21', 'INPUT #22', 'INPUT #23', 'INPUT #24', 'INPUT #25', 'INPUT #26', 'INPUT #27', 'INPUT #28', 'INPUT #29', 'INPUT #30', 'INPUT #31', 'INPUT #32' }

Comments

  • Options
    mpullinmpullin Posts: 949
    You were pretty close. You just have to put an extra [] in there, i.e. VOLATILE CHAR cINPUTNAMES[32][10]

    A string is an array of chars. What you are declaring is an array of arrays of chars. What I wrote above describes an array of 32 strings and each one contains at most 10 chars.
    cINPUTNAMES[2] -> 'INPUT #2'
    cINPUTNAMES[2][8] -> '2'

    I'm assuming this was an academic example and you are going to be storing more interesting strings than 'INPUT #2' - otherwise you could just use itoa and forgo the array altogether :)
  • Options
    HedbergHedberg Posts: 671
    In my experience, declaring 2d character arrays is a bit tricky. The following seem to work:
    char sSTR[][3] = {'abc','def'}
    char sSTR2[][] = { {'a','b','c'}, {'d','e','f'} }
    
    while this does not:
    char sSTR3[][] = {'abc','def'}
    
  • Options
    Joe HebertJoe Hebert Posts: 2,159
    jabramson wrote: »
    Is it possible to do an array with strings (or characters) in it?
    Yes - Here's an example:
    CHAR cTransportMap[19][6]	= {
    
       {'PLAY'}, 	//1 
       {'STOP'}, 	//2 
       {'PAUSE'}, 	//3 
       {'SKIP+'}, 	//4 
       {'SKIP-'}, 	//5 
       {'SCAN+'}, 	//6 
       {'SCAN-'}, 	//7 
       {'RECORD'}, 	//8 
       {'POWER'}, 	//9 
       {'NUM0'}, 	//10 
       {'NUM1'}, 	//11
       {'NUM2'}, 	//12
       {'NUM3'}, 	//13
       {'NUM4'}, 	//14
       {'NUM5'}, 	//15 
       {'NUM6'}, 	//16
       {'NUM7'}, 	//17
       {'NUM8'}, 	//18
       {'NUM9'} 	//19
                      
    }
    
    
    
  • Options
    travistravis Posts: 180
    Hedberg wrote: »
    while this does not:
    char sSTR3[][] = {'abc','def'}
    
    char sSTR3[][3] = { 
      {'abc'},{def'}
    }
    
  • Options
    viningvining Posts: 4,368
    travis wrote: »
    char sSTR3[][3] = { 
      {'abc'},{def'}
    }
    
    I beleive the earlier post just eluded to the fact that the second array needs to be defined.

    [] [] won't work
    but
    [] [3] will

    When dealing with 2 or more arrays the last array needs to be defined with a number in the brackets not just the array itself.
Sign In or Register to comment.