Announcement

Collapse
No announcement yet.

Passing strings to and from functions - array pointer gets lost?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Passing strings to and from functions - array pointer gets lost?

    Hi,
    I'm a little screwed up with strings and string pointers. I have a function that generates a string, passes it back via a function return, then I need to pass the string to another function for processing. How do I make this work? The function printit below doesn't print.

    Anyone around that can help me straighten out the pointers or help with a solution?

    thanks a lot,
    Dave.

    func main()
    var temp;
    var test[60];
    gfx_Set(SCREEN_MODE,PORTRAIT_R) ;
    print("Confused about string pointers\n");
    temp:= GetErrorText(); //call a function that builds the string
    print([STR] temp); //This prints the string
    printit(temp); //I want to pass the string to this other function to print it
    repeat
    forever
    endfunc

    func printit(var test3)
    print([STR] test3); //this does not print the string here, compiler lost the pointer?
    endfunc

    func GetErrorText()
    var ErrorString[60]; //setup string
    to (ErrorString); print("How do I make this work?");
    return(ErrorString);
    endfunc




  • #2
    ErrorString is local to GetErrorText, so it doesn't exist once it returns.

    Pass the 'string' from the calling routine (this will pass the address of the string), or use a global. You could also make ErrorString private, if you like, it's up to you was to whether that appears to make the code clearer or muddier.
    Mark

    Comment


    • #3
      Thanks for the quick reply. I tried globals in my original program, and it didn't work, but does in this simplified example, so I'll track that down..

      Would you show me how to pass the string returned from GetErrorText in main to printit and use the string there? I've tried several versions and keep getting hung up.

      Comment


      • #4
        Because it's more like 'in ram', you need something like
        str_Printf()
        you also need a string pointer, see the example in the manual, I think you can use a "" format specifier
        Mark

        Comment

        Working...
        X