Home AMX User Forum NetLinx Studio

Newbie question about string vs char

Sorry for the very n00b question. I've got a lot of PHP programming experience, but am getting thown into AMX programming and am having some trouble with data types. I'm trying to define a constant to track what kind of projector I'm controlling, and was hoping to use plain text, like this: proj_type = 'epson'.

If I define the constant as type CHAR, I get the compiler warning "Converting type [string] to [CHAR]. If I define the constant as STRING, I get an error that stops the compile. Then of course farther down I get warnings or errors in my define call statements and call statements depending on how I try to define the constant.

Can someone point me to some resourses that will help me understand the nuances of CHAR vs STRING in AMX Netlinx code? I've searched the forums and tech notes, but haven't found anything that helps me understand.

Thanks so much,

Comments

  • mpullinmpullin Posts: 949
    Hi fogled. Here's my understanding of it.
    STRING is not a data type you can declare in Netlinx. To use a string, declare an array of characters, e.g. CHAR proj_type[] = 'epson';

    These arrays of characters are not vectors. CHAR proj_type[] = 'epson'; will create an array of size 5. If you later say proj_type = 'digitalprojection';, proj_type will contain 'digit'. You can change the length of a string using the SET_LENGTH_STRING function, or you could declare the string to be longer than its initial value by doing something like CHAR proj_type[25] = 'epson';

    Also keep in mind arrays in Netlinx are 1-indexed, not zero-indexed. proj_type[2] is 'p'.
  • Ah, thank you!

    And, I figured out why I was having so much trouble finding the info to begin with: I kept opening up "Netlinx Studio Help" from the Help menu, instead of "Netlinx Keywords Help". The "Keywords" has the programmers reference; the "Studio" contains lots of other stuff.

    Once I defined the constant as CHAR proj_type[] = 'epson', and made sure I put in the square brackets wherever else I was dealing with this character array, I got a compile without any errors.
  • mpullinmpullin Posts: 949
    Once I defined the constant as CHAR proj_type[] = 'epson', and made sure I put in the square brackets wherever else I was dealing with this character array, I got a compile without any errors.

    You only need to use the square brackets when you define the array, or when you want to access a specific cell. Other places in your program you should not use the brackets, just the variable name, e.g. if (proj_type == 'epson') return proj_type;
  • DHawthorneDHawthorne Posts: 4,584
    A string in NetLinx is just a CHAR array. There are some rather obscure ways of referencing them ... for example, 'a' is different than "'a'" (the first is a single CHAR, the second is a CHAR array of size 1, and it does matter for some functions which terminology you use, notably the _STRING functions).

    A warning on using SET_LENGTH_STRING though: it doesn't actually change any values, just the working size of the array. You cannot use it to allocate a larger array than how you initialized it in the first place.

    CHAR sString[25] = {'test'} has 25 bytes allocated, and if you used LENGTH_STRING on it, you would get a value of 4. If you used SET_LENGTH_STRING on it with a value of five, LENGTH_STRING would report it as 5, and you would have a null at the end.

    CHAR sString[] = {'test'} has a 4 bytes allocated, and any attempt to stuff something larger in it will only get truncated - and no compiler error either. You can't use SET_LENGTH_STRING on it with a value of 25 to grow it; initialized like that, a CHAR array can only be set smaller, not larger.

    There are no dynamic memory allocation functions in NetLinx (as much as many of us would dearly love there to be). You always have to allocate for your largest possible value.
  • Thanks for the additional info. I think the point I was missing about Netlinx is that it's very much a token-oriented environment. Most of my previous programming experience has been in very "wordy" scripting languages - database development, applescript, php, stuff like that.

    My current understanding of Netlinx is the expectation that you'll represent a text value like "epson" with a token, i.e. the numeral 1. It'll take me a bit to get used to that; I've always used the 'wordyness' of other languages as a crutch, per se, to be able to understand / remember what the variables were for.

    Anyway, my project is moving along OK now that I'm looking at the correct reference guide. I'll probably be back on the forums regularly as I learn. I'm starting with the web based training, and will eventually go to the real training and hopefully pick up my ACE qualification.

    Thanks again!
  • mpullinmpullin Posts: 949
    DHawthorne wrote:
    You can't use SET_LENGTH_STRING on it with a value of 25 to grow it; initialized like that, a CHAR array can only be set smaller, not larger.
    Thanks for clarifying that, I realize in my post I implied that you could make a string longer than it was declared by using SET_LENGTH_STRING. This is of course false.
  • You only need to use the square brackets when you define the array, or when you want to access a specific cell. Other places in your program you should not use the brackets, just the variable name, e.g. if (proj_type == 'epson') return proj_type;

    FYI - I'd like to expand on that statement above just a bit, because I got back to this issue and have tried a few more variations to make sure I understand what's going on.

    I'm passing the proj_type into a CALL, and I still have to declare the call-specific name with brackets, or I get a compile warning. But... not in the DEFINE CALL line where I would expect; i get the compile warning on the original CHAR proj_type[] = 'epson' line.

    So:

    DEFINE CONSTANT
    CHAR proj_type[] = 'epson'
    ...then...
    DEFINE CALL 'projcontrol' (CHAR ptype)
    ...where...
    CALL projcontrol (proj_type)

    produces a "converting string to CHAR" warning on the line up in DEFINE CONSTANT.

    Changing the DEFINE CALL line to add empty square brackets:

    DEFINE CALL 'projcontrol' (CHAR ptype[])

    gets rid of the compile warning.
  • DHawthorneDHawthorne Posts: 4,584
    For anyone who read my first response: I missed something in the original post and didn't really address the question, hence this edit :).

    The NetLinx compiler does a type check on your parameter, so it needs the brackets to know it is checking for an array and not a single CHAR. You don't need to put a size in, it just has to know to check for an array. Since it clearly works by passing pointers internally, this might seem unnecessary (it did to me, that's certain), but for type-checking to work, it has to be there.
  • What about a function that returns a character array?

    Lets say for example I was using a function like

    Define_Function Char SetupSerialSettings
    {
    Return 'SET BAUD 192000,N,8,1,DISABLE'
    }

    If I enter that code into Netlinx it gives the string to char warning. How would I go around getting rid of the warning?
  • alexanboalexanbo Posts: 282
    Change Char in the function header to Char[30].. or longer you'd probably want enough space to go to 115k.
Sign In or Register to comment.