Thread: void * to char *

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    6

    void * to char *

    Hi there, i got this function : and i most convert the (void*) returned by the function in a (char *). I tried many things, but nothing could go past the compiler.. can anyone give me a hint on this?

    my last try was something like: (char*)function_result
    where function_result is the void* returned by the function.

    Code:
    typedef struct
    {
      int nombre_prefere; 
      char allo[50];	  
      char *chaine_dynamique;
    } MaStructure;
    
    void *recuperateur_de_allo(void *donnees)
    {
      MaStructure *d = donnees;
      return d->allo;
    }

    I give you my big thanks!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It helps if you make a small example of what you're trying to compile.
    Code:
    #include<stdio.h>
    struct foo
    {
        char bar[ 10 ];
    };
    
    void * baz( void * foo )
    {
       return ((struct foo *)foo)->bar;
    }
    
    int main( void )
    {
        struct foo bar;
        char *fbb;
    
        fbb = (char *) baz( &bar );
        return 0;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    Sorry, your right. Here an exemple that represent my problem.

    Code:
    #include<stdio.h>
    struct foo
    {
        char bar[ 10 ];
    };
    
    void * baz( void * foo )
    {
       return ((struct foo *)foo)->bar;
    }
    
    char *main( (*baz)(void *))
    {
        struct foo bar;
    
        return = (char *) baz( &bar );
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Look at my example, and compare it to yours. What parts are you confused about?


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

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Clue: This is not how to call a function
    Code:
    return = (char *) baz( &bar );
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    at the line: return = (char *) baz( &bar );

    i get this error : 'void' term to non-'void'

    Clue: This is not how to call a function
    event with this clue i just can't see it right now . I been scripting 36 hours straight for school. heh

    Thx for helping me guys.
    Last edited by Loukoulaylay; 04-10-2009 at 04:44 PM.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why did you change that line from what I had? You must have had a reason. What was it?


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

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    i changed the "char *main( (*baz)(void *))" too. it's exactly like what i use it in my program. Since I'm still a beginner, i prefer showing an example really similar to the real thing.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Loukoulaylay View Post
    I been scripting 36 hours straight for school. heh
    In C? Are you sure? Maybe it is time to quit before you grow old!

    Just kidding. My point is you are complaining about how to typecast a return value, but you are not really using the return value at all. That would be:
    Code:
    struct foo bar;
    char ptr*;
    ptr = (char *) baz( &bar );
    return 0;
    The return value of main is not used during the execution; it's for the OS, and it should be an int, not a char. Which brings us to this:
    Code:
    char *main( (*baz)(void *))
    What is that supposed to mean???
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    In C? Are you sure? Maybe it is time to quit before you grow old!
    I must agree with that. That is the last time i take a C course as an option at University..heh

    yeah, i do know that. I just used the main for the example, but in my "real case" it's an another function which return char *main( (*baz)(void *)).

    so i guess that's not my problem , but you guys still gave my clues. My thinking wasn't all wrong since i did the same thing as you when i tried ^^.

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    In any case, "return=" is non-sensical.

    Using a (char*)typecast should be enough if you have the rest of your syntax in order.

    Next time don't wait til the end of the semester to start coding!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    lol i didn't wait till the end. i Just haev 3 big project to do during the semester .. and i just received the last one. I still got 1 week to finish it, but i wan to clear it right aways.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. msvc just ate one of my source files
    By Eber Kain in forum C++ Programming
    Replies: 6
    Last Post: 07-01-2004, 05:40 AM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  5. Searching a linked list for char
    By spentdome in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 11:11 AM