Thread: Returning a string as a char*... Help!

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    3

    Returning a string as a char*... Help!

    A freind of mine is having a bit of C problem. She's just learning C and neither of us can find out why this code simply doesn't work:
    Code:
    EXPORT char *_get(double a) { 
       char *s;
       itoa(num[(int)a],s,10);
       return s;
    }
    As you can see, it should convert an integer to a string and then return the string as a char*... Well it just doesnt work (but compiles fine). Can someone tell me what she's doing wrong?
    -Steve

  2. #2
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    aha! i had just about the exact problem a few hours ago.. but here's how i solved it.

    use a pointer to char to accept the return value.
    Code:
    #define MAX 20 /* change MAX as you please */
    /*the usual # includes here */
    int main void() {
         char *result;
         double a=5.0;
         result=malloc(MAX * sizeof(char));
         result=_get(a);
         return 0;
    }
    and in your get() function, malloc that *s too. hehehe! i dont know, but it solved my problem here, which is the same as your's. im still a newbie though.
    It is not who I am inside but what I do that defines me.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >and in your get() function, malloc that *s too.
    That's one too many. You allocate memory to result, then assign a reset it to a different address, thus losing the memory you just allocated.

    >char *s;
    s is a pointer, but to where? If you said "I dunno", then you're correct Before you use a pointer, make sure that it points to memory that you own.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    3
    Thanks to the both of you she got it to work like a charm .
    Very much appreciated.
    -Steve

  5. #5
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    so prelude, which works? i malloc() the local char pointer and then let the char pointer (not malloc()'ed) in main accept it or i malloc the char pointer in main, not the local char pointer. which is which?
    It is not who I am inside but what I do that defines me.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >so prelude, which works?
    You can malloc the pointer in main and then pass it to the function (freeing it in main when you're done), or you can malloc the pointer in the function and return it to main (also freeing it in main when you're done):
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void test1 ( char *p )
    {
      strcpy ( p, "test" );
    }
    
    char *test2 ( void )
    {
      char *p = malloc ( 5 );
    
      strcpy ( p, "test" );
    
      return p;
    }
    
    int main ( void )
    {
      char *p1;
      char *p2;
    
      p1 = malloc ( 5 );
      test1 ( p1 );
      puts ( p1 );
      free ( p1 );
    
      p2 = test2();
      puts ( p2 );
      free ( p2 );
    
      return 0;
    }
    The former is recommended over the latter because it keeps the allocation and deallocation at the same level. It's harder to forget to free when you can look for a malloc in the same function.
    My best code is written with the delete key.

  7. #7
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    So it's better to malloc in main()? hmmm. So, are those that i've malloc()'ed in freed after every function call? or are they simply left there until the program ends? what is the lifetime of local malloc()'ed variables?
    It is not who I am inside but what I do that defines me.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >what is the lifetime of local malloc()'ed variables?
    The lifetime is from the time you call malloc to the time you call free.
    My best code is written with the delete key.

  9. #9
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    So what happens to those to those malloc()'ed variables after program termination? i thought that all variables declared on the program would be "cleaned up". darn!
    It is not who I am inside but what I do that defines me.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If your OS is nice, it cleans them up. If not, you crap all over the place.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    my OS is winXP service pack 2. so does it have "cleaning up" features?
    It is not who I am inside but what I do that defines me.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    *shrug* Who cares? Clean up after yourself. Don't write crappy code.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by sangken
    aha! i had just about the exact problem a few hours ago.. but here's how i solved it.

    use a pointer to char to accept the return value.
    Code:
    #define MAX 20 /* change MAX as you please */
    /*the usual # includes here */
    int main void() {
         char *result;
         double a=5.0;
         result=malloc(MAX * sizeof(char));
         result=_get(a);
         return 0;
    }
    and in your get() function, malloc that *s too. hehehe! i dont know, but it solved my problem here, which is the same as your's. im still a newbie though.
    What is "int main void"?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  14. #14
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    the FAQ in this forums says that main() returns an int. im just following their example. the exact reason, i dont know for myself.
    It is not who I am inside but what I do that defines me.

  15. #15
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Then why are you using VOID?

    It looks like this at the moment, the main function name is "void()", ant it's return type is "int main"?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM