#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);
}

Recommended Answers

All 4 Replies

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);;
}

#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);
}

#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.

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]?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.