Thread: unknown size for type 'void'

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

    unknown size for type 'void'

    So, how do I do something like cast a void to an int?

  2. #2
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    499
    Why do you want to cast a void into an int in the first place? If it's a void pointer to an int, that is a serious design flaw as void* pointers are avoided whenever possible.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by joeprogrammer
    Why do you want to cast a void into an int in the first place? If it's a void pointer to an int, that is a serious design flaw as void* pointers are avoided whenever possible.
    Maybe he wants to dereference a void pointer

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    21
    I wanted to use it for a malloced pointer.

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    21
    Quote Originally Posted by Tonto
    Then is there another way to do dynamic memory allocation?

  7. #7
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You should not need to cast malloc if you are using a modern C compiler. If you are compiling on a C++ setting, you would have to cast malloc, and would be posting on the C++ boards, and then probably using new/new[] anyways. All you would probably have to do with your IDE to compile it as a C program would be to change the extension from .cpp to .c

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    21
    I'm not compiling with any C++ stuff.
    But... I don't think that answered my question. If it did, then I didn't understand it.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > I wanted to use it for a malloced pointer.
    As in call malloc, assign to a pointer of the correct type and subscript?
    Code:
    int *p = malloc( 10 * sizeof *p );
    for ( i = 0 ; i < 10 ; i++ ) p[i] = 0;
    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.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    He never said anything about casting malloc. I assume they've got something like so:
    Code:
    void *foo;
    
    foo = malloc( sizeof( int ) );
    And they want to now know how to use that integer. Thus, the question of 'how do I typecast a void pointer to an int'. The simple answer they were waiting for is:
    Code:
     *((int *)foo) = 10;
    Cast to an integer pointer, since you are in fact using a pointer, then dereference it to make an assignment, or what not.


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

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    21
    Quote Originally Posted by quzah
    He never said anything about casting malloc. I assume they've got something like so:
    Code:
    void *foo;
    
    foo = malloc( sizeof( int ) );
    And they want to now know how to use that integer. Thus, the question of 'how do I typecast a void pointer to an int'. The simple answer they were waiting for is:
    Code:
     *((int *)foo) = 10;
    Cast to an integer pointer, since you are in fact using a pointer, then dereference it to make an assignment, or what not.


    Quzah.
    Many thanks

  12. #12
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Quote Originally Posted by quzah
    Code:
     *((int *)foo) = 10;
    Can you please explain this casting step by step?

    Thank you !
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  13. #13
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    It casts the void* foo to an int*, and then dereferences it and assigns it to 10.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  4. Generic heapsort
    By Sephiroth1109 in forum C Programming
    Replies: 15
    Last Post: 12-07-2007, 06:14 PM
  5. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM