954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Simply question: How do I return a string from a function

#include
#include
char* string(char *str);
int main()
{
printf("%s\n ",string(" in this method "));
getchar();
return 0;
}

char *string(char *str4)
{
char str[25] = "How";
char str2[25] = " do I ";
char str3[25] = " return a string";
strcat(str,str2); //append str2 to str and return str

//strcat(str,str3); //append str3 to str and return str
//strcat(str,str4); This line cause the program to close at runtime with an error


return strcat(str,str3);
}

Fabii23
Newbie Poster
8 posts since Sep 2006
Reputation Points: 13
Solved Threads: 0
 

I get a warning about returning a local variable addresss if I return str. the program prints nothing at runtime if I execute the code below


#include
#include
char* string(char *str);
int main()
{
printf("%s\n ",string(" from this method "));
getchar();
return 0;
}
char *string(char *str4)
{
char str[25] = "How";
char str2[25] = " do I ";
char str3[25] = " return a string";
strcat(str,str2); //append str2 to str and return str

return strcat(str,str3);;
}

Fabii23
Newbie Poster
8 posts since Sep 2006
Reputation Points: 13
Solved Threads: 0
 

#include
#include
char* string(char *str);
int main()
{
printf("%s\n ",string(" in this method "));
getchar();
return 0;
}
char string(char *str4)
{
char str[25] = "How";
char str2[25] = " do I ";
char str3[25] = " return a string";
strcat(str,str2); //append str2 to str and return str

return strcat(str,str3);
}

Fabii23
Newbie Poster
8 posts since Sep 2006
Reputation Points: 13
Solved Threads: 0
 
#include <stdio.h>
#include <string.h>
char* string(char *str);
int main()
{
      printf("%s\n ",string(" in this method "));
      getchar();
      return 0;
}
 char string(char *str4) // the char should be char*
{
     char str[25]  = "How";
     char str2[25] = " do I ";
     char str3[25] = " return a string";
     strcat(str,str2);    //append str2  to str and return str
return strcat(str,str3); // you are returning the addr of local variable
                                 // whose scope is limited to this function.
}

Dont return the addr of a local variable since local vars of a function are placed on the stack and are destroyed when the function returns.

Better pass the string which you want to be modified to the function.
Something like this:

#include <stdio.h>
#include <string.h>
char* my_string (char *src, char* dest);
int main()
{
    char str[25] = {'\0'} ;
    printf("%s\n ",my_string(" in this method ", str));
    getchar();
    return 0;
}

char* my_string(char *src, char* dest)
{
    char str1[25] = "How";
    char str2[25] = " do I ";
    char str3[25] = " return a string";
    strcat(dest,str1);   //append str2 to str and return str
    strcat(dest,str2);
    strcat(dest,str3);
    return dest ;
}


Hope it helped, bye.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Please don't be so cute with your questions. Ask it directly so we don't have to figure out what you want to know. The code is supposed to show us what you've tried, not be the question.
#include
#include
char* string(char *str);
int main()
{
printf("%s\n ",string(" in this method "));
getchar();
return 0;
}

char *string(char *str4)
{
char str[25] = "How";
char str2[25] = " do I ";
char str3[25] = " return a string";
strcat(str,str2); //append str2 to str and return str

//strcat(str,str3); //append str3 to str and return str
//strcat(str,str4); This line cause the program to close at runtime with an error


return strcat(str,str3);
}
Basically, you can't do it this way.
1) when you return, the stringstr is deleted, therefore there's no data left.
2) strcat() returns a pointer, not the string. The pointer returns but the string has been deleted. See 1.

The way to return the string is define the string in the calling routine and pass the string into the function.

As for the crash, how many total characters did you load intostr[25]?

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You