/* * * Demonstrate using a void pointer to pass different types to a function */ #include void half(void *, char); void main() { /* Initialize one variable of each type */ int i = 20; long l = 100000; float f = 12.456; double d = 123.044444; /* Display their initial values */ printf("\n%d", i); printf("\n%ld", l); printf("\n%f", f); printf("\n%lf\n\n", d); /* Call half () for each variable. */ half(&i, 'i'); half(&l, 'l'); half(&d, 'd'); half(&f, 'f'); /* Display their new values */ printf("\n%d", i); printf("\n%ld", l); printf("\n%f", f); printf("\n%lf\n", d); } void half (void *x, char type) { /* Depending on the value of type, cast the * pointer x appropriately and divide by 2. */ switch (type) { case 'i': { *((int *) x) /= 2; break; } case 'l': { *((long *) x) /= 2; break; } case 'f': { *((float *) x) /= 2; break; } case 'd': { *((double *) x) /= 2; break; } } }