Using type void pointers, depending on the value of type, cast the pointer x appropriately and divide by 2 - C Function

C examples for Function:Function Parameter

Description

Using type void pointers, depending on the value of type, cast the pointer x appropriately and divide by 2

Demo Code

#include <stdio.h>

void half(void *pval, char type);

int main( void )
{
    int i = 20;//from w  w  w. jav  a 2s.c o  m
    long l = 123456;
    float f = 12.456;
    double d = 123.044444;

    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);
    return 0;
}

void half(void *pval, char type)
{
    switch (type)
    {
        case 'i':
            {
            *((int *)pval) /= 2;
            break;
            }
        case 'l':
            {
            *((long *)pval) /= 2;
            break;
            }
        case 'f':
            {
            *((float *)pval) /= 2;
            break;
            }
        case 'd':
            {
            *((double *)pval) /= 2;
            break;
            }
    }
}

Related Tutorials