Converting VOID* to INT ON C/C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jose Diaz
    New Member
    • Nov 2010
    • 6

    Converting VOID* to INT ON C/C++

    In the past couple of days i have noticed that a lot of people are having problems casting from one data type to another specially from void to char or int.

    First of all void* just means that the variable can pretty much hold any type of data, from character all the way unto float. Let me just show you >>

    Lets say you want void *variable;
    to be equal 100;

    Code:
    void *variable = 100;
    Now this is just going to give you a warning telling you
    you did not cast it unto an integer (int).

    In order to do that you will do it like this>>

    Code:
    void *variable =(int *) 100;
    Now lets work with character(char) >>

    Code:
    char *variable =(char *)"This is a message";

    Now am going to show how to do it from a void function unto a int function>>

    Code:
    void *var(void *msg){printf("%s",msg);}
    A function call var with an void parameter, Now you
    may wonder how do i convert that to an integer(int) function.
    Well its very easy >>

    Code:
    char *function=(char *)var("hello world!\n");
    First i declare a new variable with data type of int
    as pointer then set it equal to an integer function by casting it, then feeding that parameter inside var().


    AGAIN!!! That was easy.




    I use Dev-C++ 4.9.9.2 if you are having any problems
    writing or debugging any program feel free to contact me i will gladly help you. =)

    ** Edit ** email removed


    -JOEY
    Last edited by NeoPa; Nov 18 '10, 11:48 PM. Reason: Removed illegal email and added CODE tags.
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    In C, a void* variable holds a generic data pointer. That is, a void* variable can hold a pointer to any data type. Consider some of the casts recommended by the OP.

    Code:
    void *variable =(int *) 100;
    This creates a data pointer with the same bit pattern as the integer "100". The compiler offers no assurance that this pointer can be safely dereferenced.

    Code:
    void *variable =(char *)"This is a message";
    I'm surprised this doesn't provoke a compiler error. Perhaps the pointer ends up pointing at this string; perhaps the pointer has the same bit pattern as the first few characters of the string.

    Code:
    void *var(void *msg){printf("%s",msg);}
    int *function=(int *)var("hello world!\n");
    I'm surprised this doesn't provoke a compiler error. At best, these instructions interconvert data and function pointers. The C Standard explicitly warns that data pointers and function pointers are not guaranteed to be interconvertabl e.

    First of all void just means that the variable can pretty much hold any type of data, from character all the way unto float. Let me just show you
    This sounds like the Variant type that I've seen in other languages. C definitely does not have a Variant type; I don't know about C++.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      In C++ you cast only because a) you are calling legacy C code that has maybe a void* argument or b) your C++ design is screwed up.

      C++ implements various techniques to avoid a typecast.

      Comment

      Working...