Declare variable type void *

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • farmerdoug
    New Member
    • May 2008
    • 3

    Declare variable type void *

    If I declare a variable to be type void * how do I assign values to it (int, float, and char ) and how do I read the variable.

    Thanks
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    You cannot declare variables of type void

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      void means no type. A void* describes a pointer variable that points to no type. That is, it is just an address.

      You should be able to assign the address of any type to void* but you shoule get errors assgining the address in a void* to a pointer to a real type unless you typecast.

      In any case, you cannot assign the value of an int, float, etc to a void* since those types contain actual values and not addresses.

      The above comment applies to C. In C++ you do not use void*. There are templates for that.

      Comment

      • farmerdoug
        New Member
        • May 2008
        • 3

        #4
        More info: I have a function
        int ffpky(fitsfile *fptr, int datatype, char *keyname, void *value,
        char *comm, int *status);
        The data type tells the routine what it's getting: int, float etc.
        In the calling program which I'm pretty sure is in c: How do I get an int, float, or string into value.

        thanks.

        Comment

        • farmerdoug
          New Member
          • May 2008
          • 3

          #5
          Thanks. I think I got it.



          Originally posted by weaknessforcats
          void means no type. A void* describes a pointer variable that points to no type. That is, it is just an address.

          You should be able to assign the address of any type to void* but you shoule get errors assgining the address in a void* to a pointer to a real type unless you typecast.

          In any case, you cannot assign the value of an int, float, etc to a void* since those types contain actual values and not addresses.

          The above comment applies to C. In C++ you do not use void*. There are templates for that.

          Comment

          Working...