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

Closed Thread

Join Date: Sep 2006
Posts: 6
Reputation: Fabii23 is an unknown quantity at this point 
Solved Threads: 0
Fabii23 Fabii23 is offline Offline
Newbie Poster

Simply question

 
0
  #1
Sep 23rd, 2006
#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)
{
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);
}
Quick reply to this message  
Join Date: Sep 2006
Posts: 6
Reputation: Fabii23 is an unknown quantity at this point 
Solved Threads: 0
Fabii23 Fabii23 is offline Offline
Newbie Poster

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

 
0
  #2
Sep 23rd, 2006
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 <stdio.h>
#include <string.h>
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);;
}
Quick reply to this message  
Join Date: Sep 2006
Posts: 6
Reputation: Fabii23 is an unknown quantity at this point 
Solved Threads: 0
Fabii23 Fabii23 is offline Offline
Newbie Poster

Ignore the double semicolon, that was a cut and paste error

 
0
  #3
Sep 23rd, 2006
#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)
{
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);
}
Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Ignore the double semicolon, that was a cut and paste error

 
0
  #4
Sep 23rd, 2006
#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:

  1. #include <stdio.h>
  2. #include <string.h>
  3. char* my_string (char *src, char* dest);
  4. int main()
  5. {
  6. char str[25] = {'\0'} ;
  7. printf("%s\n ",my_string(" in this method ", str));
  8. getchar();
  9. return 0;
  10. }
  11.  
  12. char* my_string(char *src, char* dest)
  13. {
  14. char str1[25] = "How";
  15. char str2[25] = " do I ";
  16. char str3[25] = " return a string";
  17. strcat(dest,str1); //append str2 to str and return str
  18. strcat(dest,str2);
  19. strcat(dest,str3);
  20. return dest ;
  21. }

Hope it helped, bye.
I don't accept change; I don't deserve to live.
Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Simply question

 
0
  #5
Sep 23rd, 2006
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.
Originally Posted by Fabii23 View Post
#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)
{
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 string str 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 into str[25]?
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Quick reply to this message  
Closed Thread

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC