In this post I will show you two method to return a string from a generic function.

First one method use a variable global as string

 char GlobalVar[20];     //dummy string variable
 char *toGlobalVar()
 {
    strcpy(GlobalVar,"Hello Main I'm GlobalVar :)");
    return GlobalVar;
 }

Second one method, used an argument of a function as char pointer

  char *toLocalVar(char *txt)
 {
    strcpy(txt,"Hello Main I'm LocalVar :)");
    return txt;
 }

How this works……

 void main()
 {
    char txt[20];//local variable
    printf("return toGlobalVar <%s>\n",toStr());
    printf("return toLocalVar <%s>\n",toStr2(txt));
 }
Advertisements