Return a string

This is a discussion on Return a string within the C Programming forums, part of the General Programming Boards category; Hi there; I want to know how to get a function to return a string in C. I know it ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    22

    Return a string

    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?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    Study how fgets() works.
    You pass the array where you want the answer stored, and a length to indicate how big the array is.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,526
    Functions will return a string literal without causing a memory leak.
    Code:
    return "This is a string returned by foo()";
    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.

    But yeah to return a string return pointer to char: char*.
    Last edited by whiteflags; 05-07-2006 at 09:36 AM. Reason: font

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    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.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by jafet
    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:
    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;
    }

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Maragato
    maybe something like this helps:
    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:
    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

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by Maragato
    maybe something like this helps:
    Code:
    #include <stdio.h>
    
    
    char *returnsArray(void){
    	char *foo = "bar";
    
    	return foo;
    }
    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 jafet
    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.
    You don't have to copy it unless you want to.
    Last edited by Quantum1024; 05-08-2006 at 07:53 AM.

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Quantum1024
    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.
    You don't have to copy it unless you want to.
    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...

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    Except that it's only a very short step to chaos and madness when you do this
    Code:
    char *returnsArray(void){
    	char foo[] = "bar";
    	return foo;
    }
    Which is of course very wrong.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    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

  11. #11
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Salem
    Except that it's only a very short step to chaos and madness when you do this
    Code:
    char *returnsArray(void){
    	char foo[] = "bar";
    	return foo;
    }
    Which is of course very wrong.
    Of course, but again we are returning pointers in the right case not arrays those are different things.

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    2,012
    You can always return a structure with only a string in it.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    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
    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.

  15. #15
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,526
    ... 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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  4. Replies: 4
    Last Post: 03-03-2006, 01:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21