Thread: getting a function to return a char array

  1. #1
    the lowly newb
    Join Date
    Jan 2005
    Location
    IL
    Posts
    49

    getting a function to return a char array

    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

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You can't pass arrays between functions. You can however put them into a structure and return a structure.

    Bad:
    Code:
    char[10] foo (void)
    {
      char bar[10] ="Hello";
      return bar;
    }
    Not Bad:
    Code:
    typedef struct {
      char bar[10];
    } Ret;
    
    Ret foo(void)
    {
      Ret r = { "Hello" }
      return r;
    }

  3. #3
    the lowly newb
    Join Date
    Jan 2005
    Location
    IL
    Posts
    49
    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

  4. #4
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    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;
    }
    The code you posted was trying to print out the stucture and not the character string.
    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

  5. #5
    the lowly newb
    Join Date
    Jan 2005
    Location
    IL
    Posts
    49
    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

  6. #6
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    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

  7. #7
    the lowly newb
    Join Date
    Jan 2005
    Location
    IL
    Posts
    49
    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

  8. #8
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    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

  9. #9
    the lowly newb
    Join Date
    Jan 2005
    Location
    IL
    Posts
    49
    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

  10. #10
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    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

  11. #11
    the lowly newb
    Join Date
    Jan 2005
    Location
    IL
    Posts
    49
    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

  12. #12
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    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

  13. #13
    the lowly newb
    Join Date
    Jan 2005
    Location
    IL
    Posts
    49
    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

  14. #14
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    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;
    }

  15. #15
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by andyhunter
    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;
    }
    The code you posted was trying to print out the stucture and not the character string.

    With this code:

    gcc -Wall test.c
    test.c: In function `main':
    test.c:18: warning: format argument is not a pointer (arg 2)

    ..
    ..
    (Program bombs)
    However as c++:
    g++ -Wall test.c
    ..
    .. (No warnings)
    .. (Program runs to completion)
    Borland and Microsoft compilers accept C and C++ and execute to completion.

    Any comments from the experts?

    Regards,

    Dave

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM