Several points:
- You do not need the intermediary
pd
to assign the address of d
to t.val
. The following should work perfectly well:
t.val = &d;
C allows direct assignment between void *
and other pointer types. C++ does not, so this won't work in C++ without a cast:
t.val = (void *) &d;
- However, this assumes that you will use
t.val
during the lifetime of d
. If there is a chance that d
will go out of scope before you use t.val
, then you should allocate a new block of memory to store the value of d
:
t.val = malloc( sizeof d );
if ( t.val )
*(double *)t.val = d;
So, instead of storing the address of d
in t.val
, you're storing the address of a dynamically-allocated block of memory, and copying the value of d
to that dynamic block. Since t.val
is a pointer to void
, you first need to use the cast expression (double *)
to tell the compiler to treat it as a pointer to double
. Then you need to dereference that pointer with the unary *
operator to assign the value of d
to that new memory block. This also means you will need to deallocate that block when you're done with it:
free( t.val );
- Either way, you print the value that
t.val
points to as follows:
printf( "%d %f\n", t.id, *(double *)t.val );
Again, we have to tell the compiler to treat t.val
as a pointer to double
and use the unary *
operator to obtain the value that the pointer points to.
EDIT
Here's a very contrived example of what I mean about d
"going out of scope". Let's assume that we create the t
object in main
, then call one function that assigns t.val
using a local variable in that function, and then call another function to print the value that t.val
points to:
/**
* Sets the val member of a struct Table object. Since we want to
* modify the contents of the parameter, we need to pass a pointer
* to it.
*/
void assignVal( struct Table *tbl )
{
double d = some_value(); // d only exists for the duration of this function
...
tbl->val = malloc( sizeof d ); // since tbl is a pointer, use ->
if ( tbl->val ) // instead of . to access members
*(double *) tbl->val = d;
else
// error could not allocate memory for tbl->val
...
}
void printVal( struct Table tbl )
{
...
if ( tbl.val )
printf( "%d %f\n", tbl->id, *(double *) tbl.val );
else
// tbl.val was never allocated
...
}
int main( void )
{
struct Table t;
assignVal( &t );
printVal( t );
if ( t.val )
free( t.val );
}
Since we want assignVal
to modify the contents of t
, we need to pass a pointer to it as the argument. The syntax tbl->val
is the same as (*tbl).val
- we dereference tbl
before attempting to access the val
member.
Since d
ceases to exist as soon as assignVal
exits, we need to allocate a new object to store d
's value. Once we are finished with that value, we free t.val
.
In this example, assignVal
and printVal
both assume that t.val
points to a double
object. For more general-purpose code, you may want to add another member that keeps track of the type of object val
points to.
For example:
struct Table {
int id;
int valueType;
void *val;
};
void assignDblVal( struct Table *tbl )
{
double d = ...;
...
t->valueType = DOUBLE; // where DOUBLE is some kind of integer constant
t->val = malloc( sizeof d );
if ( t->val )
*(double *) t->val = d;
...
}
void assignIntVal( struct Table *tbl )
{
int x = ...;
...
tbl->valueType = INT;
tbl->val = malloc( sizeof x );
if ( tbl->val )
*(int *) tbl->val = x;
...
}
void printVal( struct Table tbl )
{
switch( tbl.valueType )
{
case CHAR:
printf( "%d '%c'\n", tbl.id, *(char *) tbl.val );
break;
case INT:
printf( "%d %d\n", tbl.id, *(int *) tbl.val );
break;
...
case DOUBLE:
printf( "%d %f\n", tbl.id, *(double *) tbl.val );
break;
...
}
}
This way, printVal
doesn't have to assume that tbl.val
points to a double - we tell it what type it points to, and it uses the right cast and format specifier to do the printing.
Bear in mind, these are quick-n-dirty examples. There are much cleverer ways of dealing with type polymorphism in C. I just can't think of one right now.