| | |
Simply question: How do I return a string from a function
![]() |
•
•
Join Date: Sep 2006
Posts: 6
Reputation:
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)
{
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);
}
#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);
}
•
•
Join Date: Sep 2006
Posts: 6
Reputation:
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 <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(" 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);;
}
•
•
Join Date: Sep 2006
Posts: 6
Reputation:
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)
{
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 <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:
C Syntax (Toggle Plain Text)
#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.
I don't accept change; I don't deserve to live.
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.
Basically, you can't do it this way.
1) when you return, the string str is deleted, therefore there's no data left.
2)
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]?
•
•
•
•
#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);
}
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- question about php string function (PHP)
- Validation with js of two dimensional array (JavaScript / DHTML / AJAX)
- Escape and Unescape / Handling (C)
- how do you reverse a linked list using recursion and return it as a string? (Java)
- reading from a file with python (Python)
- Error getting forms to send information (PHP)
Other Threads in the C Forum
- Previous Thread: help me ? Application crashing before main
- Next Thread: want a programme in c
| Thread Tools | Search this Thread |
#include * adobe ansi array arrays asterisks binarysearch centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc createprocess() database dynamic execv feet fgets file floatingpointvalidation fork function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram inches include incrementoperators infiniteloop input interest intmain() iso kernel keyboard kilometer km license linked linkedlist linux locate looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation owf pattern pdf performance pointer posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprograming socketprogramming standard strchr string suggestions systemcall test threads turboc unix urboc user variable voidmain() wab whythiscodecausesegmentationfault windowsapi





