Hi there;
I want to know how to get a function to return a string in C. I know it cannot be done by simplying returning a character array, any ideas?
Printable View
Hi there;
I want to know how to get a function to return a string in C. I know it cannot be done by simplying returning a character array, any ideas?
Study how fgets() works.
You pass the array where you want the answer stored, and a length to indicate how big the array is.
Functions will return a string literal without causing a memory leak.
It's often better to just pass a string into a function and return it. To return a string you must return a pointer to char: char* foo( ...) This makes the calling function responsible for memory clean up, which is good because your string will never fall out of scope.Code:return "This is a string returned by foo()";
But yeah to return a string return pointer to char: char*.
You can make your function my_fn() return char*, then do something similiar to strcpy(target, my_fn()) in your calling code, to store the string value.
maybe something like this helps:Quote:
Originally Posted by jafet
Code:#include <stdio.h>
char *returnsArray(void){
char *foo = "bar";
return foo;
}
int main (void){
char *quux;
quux= returnsArray();
printf("%s\n",quux);
return 0;
}
Edit:Quote:
Originally Posted by Maragato
Just to be a purist there is no string type in C afaik. Just downloaded C99 docs and they mention valid type specifiers as: void, char, short, int, long, float, double, signed, unsigned, _Bool, _Complex
You should set the return type to const char* since that is what you're returning then you can place this function where an argument of type const char* is expected.Quote:
Originally Posted by Maragato
You don't have to copy it unless you want to.Quote:
Originally Posted by jafet
This was just a sample to solve his hurry I don't see anything really wrong on my function I just took over all the overhead yeah you can qualify it as const but I assume he will want to change something in the array else he would just copy it no needing for return...Quote:
Originally Posted by Quantum1024
Except that it's only a very short step to chaos and madness when you do this
Which is of course very wrong.Code:char *returnsArray(void){
char foo[] = "bar";
return foo;
}
I think that the problem is a string can be of variable length which
is a problem because the compiler needs to know how long it is.
Without knowing the length (which it can't) it would not know
how much space to allocate to store the string on the stack.
Thus it can't be don't unless there is a special provision for it
in C, which I don't think there is. However I could be totally
wrong about this.
Anyway the bottom line is there is no need to return a string,
I never have.
Just make all your variables global, like I do :)
Of course, but again we are returning pointers in the right case not arrays those are different things.Quote:
Originally Posted by Salem
You can always return a structure with only a string in it.
You can return an array that way, provided it is static. Ever hear of strtok?Code:char *foo( ... )
{
static char buf[ BUFSIZ ];
...
return buf;
}
Quzah
C strings are null-terminated, and you can count how many characters there are before the null byte. Making all your variables global is really wierd to say the least.Quote:
I think that the problem is a string can be of variable length which
is a problem because the compiler needs to know how long it is.
Without knowing the length (which it can't) it would not know
how much space to allocate to store the string on the stack.
Thus it can't be don't unless there is a special provision for it
in C, which I don't think there is. However I could be totally
wrong about this.
Anyway the bottom line is there is no need to return a string,
I never have.
Just make all your variables global, like I do
... and poor practice. Making variables global means that they can be changed anywhere within the program, if something goes wrong with that data it can be difficult to find exactly where it all fell apart.