Home AMX User Forum NetLinx Studio

Define_constant - Char

jjamesjjames Posts: 2,908
How can this compile and still send the correct text to the panel? If you throw this into Studio, you'll see that it does contains a keyword (On Broadway) and a logical operation (Nashville!). And with Hank's Place, there's a single quote, making a good portion a string literal.

I'm using the "'!T',100,'TEXT'" command to send text to the panel. It all works (so I'm not complaining), but how does it work?

If I make Hank's Place like this:
,"'Hank',39,'s Place'"
It will display exactly that on the panel: 'Hank',39,'s Place'

I'm probably overlooking something really stupid. My problem is, if I were to move Hank's (or Frank's) place to the last value, it will make the rest of my code (including BUTTON_EVENTS, DATA_EVENTS, etc) appear as a STRING by the editor's preference. The funny thing is, my code still works. Is this a Studio bug?
DEFINE_CONSTANT
CHAR CHAN_NAME[67][30]=
{
	 "The 40s"				
	,"The 50s"				
	,"The 60s"				
	,"The 70s"              
	,"The 80s"             	
	,"The 90s"              
	,"America"              
	,"Nashville!"           
	,"X Country"            
	,"Hank's Place"         
	,"Bluegrass Junction"   
	,"Highway 16"           
	,"Top 20 on 20"         
	,"KISS"                 
	,"MIX"                  
	,"The Heart"            
	,"Sunny"               	
	,"The Blend"            
	,"Cinemagic"            
	,"The Fish"             
	,"Spirit"               
	,"Deep Tracks"          
	,"XM Cafe"              
	,"Top Tracks"           
	,"Ethel"                
	,"Squizz"               
	,"The Loft"             
	,"Lucy"                 
	,"Soul Street"          
	,"The Flow"             
	,"Suite 62"             
	,"The Rhyme"            
	,"RAW"                  
	,"The City"             
	,"Special X"            
	,"Real Jazz"            
	,"Watercolors"          
	,"Beyond Jazz"          
	,"Frank's Place"        
	,"Bluesville"           
	,"Audio Vision"         
	,"The Move"             
	,"BPM"                  
	,"The System"           
	,"Chrome"               
	,"World Zone"           
	,"The Joint"            
	,"XM Classics"          
	,"VOX"                     
	,"XM Pops"
	,"The Village"
	,"On Broadway"
	,"U-Pop"
	,"Eye"
	,"Enlighten"
	,"Boneyard"
	,"XMU"
	,"XM Music Lab"
	,"Fred"
	,"The Groove"
	,"Fine Tuning"
	,"Radio Disney"
	,"XM Kids"
	,"Alegria"
	,"Caliente"
	,"MLB Home Plate"
	,"High Voltage"
}          

Comments

  • Spire_JeffSpire_Jeff Posts: 1,917
    If I'm not mistaken, shouldn't it be:

    "'!T',100,'Hank',39,'s Place'"

    Change the double quotes to single quotes and add a double quote to the end of the each string.

    Jeff
  • jjamesjjames Posts: 2,908
    I have no problem sending text to the panel, it all works. My list is what worries me. Copy that into Studio, and you should see what I mean. The weird thing is, in my list it's ,"Hank's Place", and it actually displays Hank's Place on the panel without having to use the the HEX 39 for the apostrophe. I'm just observing that it compiles with keywords in the list and it isn't a string literal.
  • Spire_JeffSpire_Jeff Posts: 1,917
    Ohh, I got what you are saying. My guess would be that it has to do with the different rules applied to declaring string constants.
  • GasHedGasHed Posts: 31
    So, the actual code for the SEND_COMMAND is:

    SEND_COMMAND "'!T',100,CHAN_NAME[X]"

    right? I see what you mean about the ' in Hank's place causing the rest of the code to look like a string. I'm surprised it works with the why Studio is so picky about how you define and create strings. My guess is that the command must assume everything after the address is a string.

    Pat
  • jjamesjjames Posts: 2,908
    GasHed wrote:
    So, the actual code for the SEND_COMMAND is:

    SEND_COMMAND "'!T',100,CHAN_NAME[X]"
    Essentially, yes.

    I'm guessing it's somewhat of a bug in Studio. I very well could be wrong though. The strange thing is, if I were to move Hank's Place to the bottom of the array, it would make the rest of my code to appear as a string, so all of my BUTTON_EVENTs, DATA_EVENTs . . . look like a string in code. And it still works and compiles fine! That's what's even more strange.
  • HedbergHedberg Posts: 671
    I think that the problem lies in that you shouldn't try to use string expressions in DEFINE_CONSTANT . To identify a string literal, single quotes are typically used. It looks to me like the compiler is interpreting the double quotes as single quotes.

    To do what you want to do, I think you need to do something like:
    DEFINE_CONSTANT
    
    CHAR sCharName[67][30] =
    {
    	'abcdefg',
    	'hijklmnop',
    	{'h','a','n','k',39,'s',' ','p','l','a','c','e'},
    	'qrstuvw'
    }
    
    

    The above gives me an array of strings (that I can examine in debug) the first four of which are what would be expected by looking at the code.
  • jjamesjjames Posts: 2,908
    Harold,

    Thanks for the suggestion . . . I just can't swallow typing in all of that, even though it would solve the "problem" of the formatting of the rest of the code. Since it's a module, I'll probably be the only one seeing the source code, so I guess it's not that big of a deal. Though I will keep the suggestion in mind.

    I just thought it was quite strange what I was seeing, and hoping others could verify.
  • HedbergHedberg Posts: 671
    Yes, I've verified what you are seeing and the behavior is not the behavior that I would have expected. Typically (as far as I understand the language), string expressions are enclosed in double quotes and string literals are enclosed with single quotes. something like:

    CHAR sString[] = "Testing"

    I would not expect this to work. In fact, If I make that declaration in define_variable or try to make such an assignment in define_start, it does not work. But, in define_constant, the above seems to have the exact same result as what I would consider appropriate:

    CHAR sString[] = 'Testing'

    So, I think we are seeing a feature of the define_constant section of which I, certainly, was not aware. In define_constant, it appears that you can create string literals by enclosing strings in double quotes with all the characters within the double quotes being represented exactly in the resulting string. A single quote or comma within the double quotes appears in the resulting string as a single quote or a comma. Reserved words are interpreted as ASCII characters and not as reserved words. BUT, typical string expression syntax does not give the expected result.
  • jjamesjjames Posts: 2,908
    Harold,

    Great work. Now here's the $64k question . . . is this a good thing or a bad thing? I can see it being good as long as it didn't mess with the appearance of the following code. If AMX could fix this visual problem, I'd be happy with it. Otherwise, it's a pain to to code with the editory all messed up.
  • DHawthorneDHawthorne Posts: 4,584
    It's not a bug in the compiler, it's a bug in the syntax highlighter. But that said, I believe the recommended way of populating char arrays is:
    DEFINE_CONSTANT
    CHAR CHAN_NAME[67][30]=
    {
    	 {'The 40s'}
    	,{'The 50s'}				
    	,{'The 60s'}				
    	,{'The 70s'}              
    	,{'The 80s'}             	
    	,{'The 90s'}              
    	,{'America'}              
    	,{'Nashville!'}           
    	,{'X Country'}  
    	,{'Hank', 39, 's Place'}      
     ... etc.
    

    Personally, I wouldn't put it in DEFINE_CONSTANT either. I'd declare it as a CONSTANT CHAR[] in DEFINE_VARIABLE. I'm not certain the above will work in DEFINE_CONSTANT (and I can't really test it right now, I'm at a customer's just waiting on a SAT receiver guide download ... ).
  • HedbergHedberg Posts: 671
    Dave, I tried your suggestion and if it's in DEFINE_CONSTANT or DEFINE_VARIABLE, I get the same result which is"

    CHAN_NAME[10] is a length 3 string " H's "

    the compiler warns that a string is being cast to a character.

    To do this in the DEFINE_VARIABLE section, I think you have to specify each character individually like:

    {'H','a','n','k',39,'s',' ','P','l','a','c','e'},

    I was surprised to see that the way Jerimiah wrote the code worked. It appears to me that everywhere but in DEFINE_CONSTANT, syntax like:

    sDummy = "string"

    gives a compiler error. Either this is an intentional feature of which I was not aware or an unintentional feature. If it's the latter, I'd be wary of depending on the presence of the feature in the future.
  • HedbergHedberg Posts: 671
    jjames wrote"
    I can see it being good as long as it didn't mess with the appearance of the
    following code. If AMX could fix this visual problem, I'd be happy with it. Otherwise, it's a pain to to code with the editory all messed up.

    if you add a comment to those lines which contain an odd number of single quotes, you can limit the editor to hosing just those lines.

    for example:

    ,"Hank's Place" //'

    is less than beautiful, but it's not gross and disgusting.
  • Joe HebertJoe Hebert Posts: 2,159
    As an aside:

    If you want to use a single quote within a literal string you can escape the quote by adding another quote, in other words double single quote it. For example,

    instead of:
    cTest = ??Hank?,39,?s Place??

    or
    cTest = ??Hank?,$27,?s Place??

    you can simply use:
    cTest = ?Hank''s Place? //(that's 2 single quotes ' and ' between k and s)

    Drifting off topic:

    Jeremiah ? Do you have another double dimensioned array with the channel numbers? You asked in different thread when to use structures vs. arrays. This might be a good example of when to consider using a structure. If I need more than one dimension to describe something, I normally go right to a structure (unless I need to pass the data into a module in which case I?m stuck with multi dimensional arrays.)

    In my opinion structures are easier to read, organize, and modify. But that?s just me. I?m not saying multidimensional arrays are wrong, I just prefer structures and use them extensively.

    If I wanted to organize XM station data, I would probably do something like this:
    DEFINE_CONSTANT
    
    INTEGER nMaxXM	 = 67
    
    DEFINE_TYPE
    
    STRUCTURE _sXMStation {
    
       CHAR		Name[32]
       CHAR		Channel[4]
       INTEGER	IsFavorite
    }
    
    DEFINE_VARIABLE
    
    _sXMStation	sXMStations[nMaxXM]
    
    DEFINE_FUNCTION fnInitXM () {
    
       sXMStations[1].Name 	= 'The 40s'
       sXMStations[1].Channel 	= '4'
       sXMStations[1].IsFavorite	= 0
       
       sXMStations[2].Name 	= 'The 50s'
       sXMStations[2].Channel	= '5'
       sXMStations[2].IsFavorite	= 0
    
       //.........
       sXMStations[10].Name 	= 'Hank''s Place'
       sXMStations[10].Channel	= '13'
       sXMStations[10].IsFavorite= 0
       
       //.........
       sXMStations[67].Name	= 'High Voltage'
       sXMStations[67].Channel	= '202'
       sXMStations[67].IsFavorite= 1
       
    }
    
    DEFINE_START
    
    fnInitXM()
    

    I know this doesn?t answer the original question you posted. I just wanted to offer an alternative point of view. Not right, not wrong, just my take on it.

    Regarding the CHAR array, I don?t know what Netlinx is doing but it sure seems wrong and doesn?t sit well in my stomach even though it ?works?. I?m with Harold and would be wary of using it in case it?s ?fixed? later.
  • jjamesjjames Posts: 2,908
    Joe Hebert wrote:
    Jeremiah ? Do you have another double dimensioned array with the channel numbers? You asked in different thread when to use structures vs. arrays. This might be a good example of when to consider using a structure.

    Joe:
    I set everything up as an array and didn't use a structure in this. The module I used this in was originally using a structure, and will admit it was easy to read. The problem was that it was written poorly, so I took it upon myself to rewrite it, and decided to use arrays. The reason why I like to learn towards using arrays rather than structures is . . . well, I just do not know enough about them, or how to use them. They do seem powerful enough to get quite a bit done, but I just haven't gotten around to looking into them, hence the other thread.

    Aside from that, since this module won't be used widespread across the country I guess I can deal with what I'm observing.
    Joe Hebert wrote:
    cTest = ?Hank''s Place? //(that's 2 single quotes ' and ' between k and s)
    I'm suprised that this will compile. I would have thought that the compiler would think it's two seperate string literals that weren't glued by the comma, and double quotes and spit up a top secret error that gives no explaination of what's wrong.

    Geez . . . I still have a lot to learn. :)
  • Joe HebertJoe Hebert Posts: 2,159
    jjames wrote:
    Geez . . . I still have a lot to learn. :)
    We all do. Tha's why it's so much fun. :)
Sign In or Register to comment.