944,045 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 41928
  • C RSS
Sep 23rd, 2006
0

Simply question

Expand 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);
}
Similar Threads
Reputation Points: 13
Solved Threads: 0
Newbie Poster
Fabii23 is offline Offline
8 posts
since Sep 2006
Sep 23rd, 2006
0

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

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);;
}
Reputation Points: 13
Solved Threads: 0
Newbie Poster
Fabii23 is offline Offline
8 posts
since Sep 2006
Sep 23rd, 2006
0

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

#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);
}
Reputation Points: 13
Solved Threads: 0
Newbie Poster
Fabii23 is offline Offline
8 posts
since Sep 2006
Sep 23rd, 2006
0

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

#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.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Sep 23rd, 2006
0

Re: Simply question

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.
Click to Expand / Collapse  Quote originally posted by Fabii23 ...
#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]?
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C Forum Timeline: help me ? Application crashing before main
Next Thread in C Forum Timeline: want a programme in c





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC