![]() |
| | #1 |
| Registered User Join Date: May 2003
Posts: 13
| now i have a most confusing problem. I have a function that returns a string (actually i have a number of them), and i want to store this string in another char array. My first thought was to try saying array = function (); but of course I found that didn't work just like string1 = string2 won't work. Then I thought of using strcpy : strcpy ( array , function() ); I'm not sure why, but I couldn't make that work either. It occured to me I could try a manual method of copying with a loop, but I couldn't see that working unless I stored function() into a variable first, which is the whole problem! So i thought screw it, no more passing arrays, i'll pass pointers. This worked for some of my functions, but for ones that initialise the array that needs to be returned within them, returning the pointer.. well, you can't return a pointer pointing to a local variable so my compiler tells me. So, in conclusion, i'm very stuck. Please help me if you know how to get around this sort of problem! Thanks. |
| turmoil is offline | |
| | #2 |
| Registered User Join Date: May 2003
Posts: 161
| You can dynamically allocate the string... Code:
char *function() {
char *str = malloc(13); // dynamically allocate a char array
if(str == NULL) {
printf("Error allocating memory for string.");
exit(1);
}
strcpy(str, "hello world!");
return str;
}
int main(void) {
char *str = function();
printf("%s\n", str);
free(str); // free the memory
return 0;
}
edit: added the statement to free the memory |
| thefroggy is offline | |
| | #3 |
| C++ Developer Join Date: Jun 2002 Location: UWaterloo
Posts: 2,718
| Or, you could make it so that the function takes the string to use and the size of the string as parameters.
__________________ Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie |
| XSquared is offline | |
| | #4 |
| Been here, done that. Join Date: May 2003
Posts: 1,036
| It would help if you posted more than the function call. You may be returning just the first character of the array for all we can tell. Post a little more of your code to give us a better reference for the problem, including the definition of the function.
__________________ There are only 10 types of people in the world -- those that use binary, and those that don't |
| WaltP is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Manipulating Character arrays in functions. | kbro3 | C++ Programming | 11 | 08-16-2008 02:24 AM |
| Selecting bits with Unions.... Not working; please help!! | dan56965 | C Programming | 12 | 08-11-2008 11:02 PM |
| MENSA Puzzle Solver: Question on char arrays and passing to/from functions | JDGATX | C Programming | 12 | 06-06-2008 05:26 PM |
| I'm having a problem with data files. | OmniMirror | C Programming | 4 | 05-14-2003 09:40 PM |
| returning arrays from functions | Leeman_s | C++ Programming | 11 | 06-05-2002 10:00 PM |