C Problem for Array of Multiple Data Type

Today's problem is related with an array, either two dimensional or three  dimensional.  The actual problem is datatype that the array stored.  For example,  in an array, first element is a double data type and second element is an integer.


To solve this problem, we shall use array of pointer.  For example, these two different data types will be store in different locations and their addresses shall be stored in our array.  The elements will be updated or retrieved by using these addresses.  See the following codes which is self explanatory.


#include <stdio.h>

#include <math.h>

#define GET_0(x)  printf("%lf \t ", * (double *) x)

#define GET_1(x)  printf("%d \t ", * (int *) x)

#define GETV(x,y) GET_##y(x)

#define MAX 2


int main() {

    double d = 2.512445; //a double data type

    int i = 202115; //an integer data type

    long *iptr[10]; //a pointer to array

    iptr[0] = (long *) &d; //get and store address of double data type to pointer

    iptr[1] = (long *) &i; //get and store address of integer data type to pointer

    int k = 0;

    while (k < MAX) {

        if (k == 0)

            GETV(iptr[k], 0); //get value at address of double data type

        if (k == 1)

            GETV(iptr[k], 1); //get value at address of integer data type

        k++;

    }

    return 0;

}


Here pointer casting is done to store address of double data type and integer data type in a pointer to array variable iptr.