getting a function to return a char array, i couldnt find any tutorials that let me return a char array from a function.
im sure its a simple task, could anyone advise me on this?
thanks
getting a function to return a char array, i couldnt find any tutorials that let me return a char array from a function.
im sure its a simple task, could anyone advise me on this?
thanks
"Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life."
"They say a little knowledge is a dangerous thing, but it's not one half so bad as a lot of ignorance."
"Light thinks it travels faster than anything but it is wrong. No matter how fast light travels, it finds the darkness has always got there first, and is waiting for it."
[all]- Terry Pratchett
You can't pass arrays between functions. You can however put them into a structure and return a structure.
Bad:
Not Bad:Code:char[10] foo (void) { char bar[10] ="Hello"; return bar; }
Code:typedef struct { char bar[10]; } Ret; Ret foo(void) { Ret r = { "Hello" } return r; }
i tried getting the example you posted to work like this and couldnt
Code:#include <stdio.h> typedef struct { char bar[10]; } pschar; pschar postchar( void ) { pschar temp = {"my pschar"}; printf("%s", temp); return temp; } int main() { printf("%s", postchar()); getch(); return 0; }
"Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life."
"They say a little knowledge is a dangerous thing, but it's not one half so bad as a lot of ignorance."
"Light thinks it travels faster than anything but it is wrong. No matter how fast light travels, it finds the darkness has always got there first, and is waiting for it."
[all]- Terry Pratchett
The code you posted was trying to print out the stucture and not the character string.Code:#include <stdio.h> typedef struct { char bar[10]; } pschar; pschar postchar( void ) { pschar temp = {"my pschar"}; printf("%s", temp.bar); return temp; } int main(void) { printf("%s", (postchar()).bar); getchar(); return 0; }
i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced
It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah
Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem
thanks, makes sense, forgot to do the .bar
that last printf wont work though, the one inside of the main
"Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life."
"They say a little knowledge is a dangerous thing, but it's not one half so bad as a lot of ignorance."
"Light thinks it travels faster than anything but it is wrong. No matter how fast light travels, it finds the darkness has always got there first, and is waiting for it."
[all]- Terry Pratchett
post your code because it is working fine for me.
i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced
It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah
Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem
Code:#include <stdio.h> typedef struct { char bar[10]; } pschar; pschar postchar( void ) { pschar temp = {"my pschar"}; printf("%s", temp.bar); return temp; } int main(void) { printf("%s", (postchar()).bar); getchar(); return 0; }
"Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life."
"They say a little knowledge is a dangerous thing, but it's not one half so bad as a lot of ignorance."
"Light thinks it travels faster than anything but it is wrong. No matter how fast light travels, it finds the darkness has always got there first, and is waiting for it."
[all]- Terry Pratchett
And what exactly is happening?
i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced
It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah
Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem
windows error
tester3.exe has encountered a problem and needs to close. We are sorry for the inconvenience.
"Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life."
"They say a little knowledge is a dangerous thing, but it's not one half so bad as a lot of ignorance."
"Light thinks it travels faster than anything but it is wrong. No matter how fast light travels, it finds the darkness has always got there first, and is waiting for it."
[all]- Terry Pratchett
hmm....interesting. Try this:
Code:#include <stdio.h> typedef struct { char bar[10]; } pschar; pschar postchar( void ) { pschar temp = {"my pschar"}; printf("%s", temp.bar); return temp; } int main(void) { pschar myStruct = postchar(); printf("\nIn main() %s", myStruct.bar); getchar(); return 0; }
i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced
It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah
Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem
that worked, thats strange.
did the previous code that i posted work for you?
"Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life."
"They say a little knowledge is a dangerous thing, but it's not one half so bad as a lot of ignorance."
"Light thinks it travels faster than anything but it is wrong. No matter how fast light travels, it finds the darkness has always got there first, and is waiting for it."
[all]- Terry Pratchett
yes, its basically the same code I originally posted in response to your post. It may appear that your compiler was causing a pointer to be returned vice a copy of the object for the non working code sample. This would thus cause you to try to access a variable that was out of scope and thus potentially (well in your case) result in shutting down your program.
Just a theory though, maybe someone else has seen this before?
i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced
It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah
Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem
cool, well thanks for the help; maybe someone else will shed some light on the situation.
"Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life."
"They say a little knowledge is a dangerous thing, but it's not one half so bad as a lot of ignorance."
"Light thinks it travels faster than anything but it is wrong. No matter how fast light travels, it finds the darkness has always got there first, and is waiting for it."
[all]- Terry Pratchett
The typical (and far more efficient) solution is for the caller to allocate memory that the function can use. If you don't know the size of the returned string before calling the function you can use malloc and return a pointer.
Code:void postchar1( char bar[] ) { strcpy(bar, "my pschar"); } char* postchar2( void ) { char* bar = malloc(sizeof("my pschar")); if (bar != NULL) strcpy(bar, "my pschar"); return bar; } int main(void) { char bar1[10]; char* bar2; postchar1( bar1 ); printf("%s\n", bar1); bar2 = postchar2(); printf("%s\n", bar2); free(bar2); return 0; }
Originally Posted by andyhunter
With this code:
However as c++:gcc -Wall test.c
test.c: In function `main':
test.c:18: warning: format argument is not a pointer (arg 2)
..
..
(Program bombs)
Borland and Microsoft compilers accept C and C++ and execute to completion.g++ -Wall test.c
..
.. (No warnings)
.. (Program runs to completion)
Any comments from the experts?
Regards,
Dave