Home AMX User Forum AMX General Discussion

char array shows empty as a whole, but i can get individual characters..

Ok for example:
local_var char myString[10];

myString = 'hello world';

send_string 0, "myString";      // nothing is printed, the output is blank

for(I=1;i<=10; I++)
    send_string 0, "myString[I]";      // this prints out each character

for(I=1;i<=10; I++){
    send_string 0, "myString[I], '     ', myString";      // this prints out each character, but no full char array.
}

I am quite confused as to why this isn't working; I can use the variable watch in debug mode, but the "myString" array never changes length from 0 or shows a value.

Am I missing something major here? ..please set me straight on this, and the code above is just a representation of what my acutual code is; I am reading in xml from a weather feed and the char array is filled character by character as a parse it from a get_buffer_char command. When I got to take the entire char array and set the elements of my weather structure, for example:
uWeather.ntemp = atoi(myString);    // nothing ever shows

I have tried numerous methods from making the char array a global variable, local_var, stack_var... I get no change. I am verifying my TKN and have tons of send_string 0's.

Thanks

Comments

  • ericmedleyericmedley Posts: 4,177
    Try putting a break point in the code so when in debug the program will stop when it executes.
  • ericmedley wrote: »
    Try putting a break point in the code so when in debug the program will stop when it executes.

    no change, im going to attempt a widechar just to see if it has any difference
  • HedbergHedberg Posts: 671
    I just tried this and it works as expected for me. The string is longer than the array length so it gets truncated, but it sends the strings to telnet as expected.
  • I figured it out.

    I probably didn't describe my problem correctly, when using the example code. I unfortunately am working on two laptops, one of which is connected to a different network and I cannot copy the code, but paraphrase it.

    As I said before, I am taking a buffer character at a time and adding it to a char array; if I print the char array out one character at a time, it outputs that character. If I try to output it as a single string, it doesn't show anything. I have found, if I initialize the character array with anything, it will then replace that what I initialized it with and output as a whole string.

    So I ended up just doing:

    local_var char myString[5];
    myString = ' ';

    ... and then as I parse information out, I replace the blank spaces and I can print myString as a whole.

    Im not totally sure why this is the case, but it fixes the problem.

    Also, widechar had no effect except having to add wc to ch conversion in the send_string 0's to avoid errors and warnings.
  • a_riot42a_riot42 Posts: 1,624
    Netlinx needs to know the length of the string if its going to do anything with it, so if you are manipulating strings yourself, the compiler won't be able to update the string length accordingly and will still think its the same old length before you manipulated it and so it won't print anything if it thinks nothing is there. You're better off to only use the Netlinx functions to manipulate char arrays so the length is always correct, or if you must manipulate them yourself you will have to use the set_length_array method and update it yourself. Unlike Java or other languages, Netlinx doesn't automatically keep track of string lengths for you in all circumstances.
    Paul
  • a_riot42 wrote: »
    Netlinx needs to know the length of the string if its going to do anything with it, so if you are manipulating strings yourself, the compiler won't be able to update the string length accordingly and will still think its the same old length before you manipulated it and so it won't print anything if it thinks nothing is there. You're better off to only use the Netlinx functions to manipulate char arrays so the length is always correct, or if you must manipulate them yourself you will have to use the set_length_array method and update it yourself. Unlike Java or other languages, Netlinx doesn't automatically keep track of string lengths for you in all circumstances.
    Paul

    If im following you correctly, if I set up a char array: myString[10], and then set it to myString = 'hello', it wont just print "hello " ? My assumption was that it would just have that empty space. Unlike in C, where the end of an array is \0 or most languages, no end is needed once a length is specified.

    If this is the case, I find it hard to believe I've never run into this problem before over the years of doing netlinx.
    Werid. Thanks for the insight!
  • a_riot42a_riot42 Posts: 1,624
    If im following you correctly, if I set up a char array: myString[10], and then set it to myString = 'hello', it wont just print "hello " ? My assumption was that it would just have that empty space. Unlike in C, where the end of an array is \0 or most languages, no end is needed once a length is specified.

    If this is the case, I find it hard to believe I've never run into this problem before over the years of doing netlinx.
    Werid. Thanks for the insight!

    I do things a certain way, so I don't always run into these issues, but I'm sure you could run a few tests to see exactly how Netlinx deals with strings if you want to figure out all its bugs/idiosyncracies. Its definitely different than C or Java. What happens with this code?
    local_var char myString[10];
    myString = 'hello world';
    set_length_array(myString, 10)
    send_string 0, myString
    

    Paul
  • Paul,

    using the set_length_array ended up working the same as me doing myString = ' '; and then adding values and printing it. I just find it so weird I've never come across this situation.

    Thank you for the info, I learn something new everyday!
  • a_riot42a_riot42 Posts: 1,624
    Thank you for the info, I learn something new everyday!

    I still come across idiosyncracies that I've never seen before. A few months ago I realized that if you declare the same module numerous times that has a structure in it holding all the device data, in a hold event, you can only access the first declared modules data. It took me a long time to debug that one!
    Paul
  • ijedijed Posts: 28
    If im following you correctly, if I set up a char array: myString[10], and then set it to myString = 'hello', it wont just print "hello " ?

    should be:

    myString = "'hello'"

    if you ever want to assign something that's longer than one element you need to use " "
    so to append to a string:

    char myString[] = 'hello' //this is ok if you are initialising an array of unspecified length


    myString = "myString, ' world'"

    sorry that's all I can explain at the moment without more coffee
  • a_riot42a_riot42 Posts: 1,624
    Strings
    A string is an array of characters of known length. This length may be less than the dimensioned length.
    
    Example:
    
    DEFINE_VARIABLE
    
    CHAR MyString[32]
    
    INTEGER StrLen
    
     
    
    DEFINE_START
    
    MyString = 'STOP'
    
    StrLen = LENGTH_STRING(MyString)
    
    In the example above, StrLen holds the value 4, the length of MyString.
    
    

    It does work in define_start though as this excerpt from the help file mentions. Personally, I think there are bugs lurking in there somewhere.
    Paul
Sign In or Register to comment.