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 <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]?
Reputation Points: 3278
Solved Threads: 894
Posting Sage
Offline 7,747 posts
since May 2006