Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to return a array string to main program

Status
Not open for further replies.

darr01

Programmer
Oct 24, 2001
28
0
0
MY
Hi guys. I have a function which needs to return a string (which is in an array)back to the main program.

The function prototype is:
char arrString(void);

char arrString()
{
char tempString1[15], tempString2[15];

statements...
statements...
strcpy(tempString1, tempString2);
return tempString1;
}

Somehow this doesn't work.

Any way how to do this? maybe using pointers?

Regards and thanks,

Darryl





 
char tempString1[15], tempString2[15];

allocates space on the call stack which is collapsed when the function returns. You need to malloc(3) space and return a pointer to the allocated space. The return should be of type char *.

The other thing that you can do is allocate the space from the caller and pass the array location to this routine (not really recommended).
 
not all compiler are able to return a local-string:

main()
{
char *arrString(char *, char *);
char tempString1[15], tempString2[15];
printf("%s\n",arrString(tempString1,tempString2));
exit(0);

}
char *arrString(char *aa, char *bb)
{
statements...
statements...
strcpy(aa, bb);
return(aa);
}


-----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
Thanks guys for the info.

Regards,

Darryl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top