I have made this code. It is working good. Can anyone please help me to make it more optimised?
Here is the code.

/*
∑∑∑∑∑∑∑∑∑∑∑∑∑∑∑∑∑∑∑∑∑∑∑∑∑∑∑∑∑∑∑∑∑
Author: Mayukh Sarkar
Email: mayukh2012@hotmail.com
Country: India
License: GPL public
Copyright ® © 
¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬
*/
#include <stdio.h>
#include <stdlib.h>
static int size = 0;
typedef enum {
  INT = 1,
  CHAR,
  FLOAT,
  DOUBLE
}dataType;

typedef union {
    int i;
    char c;
    float f;
    double d;
}container;

typedef struct {
  int type;
  container* storage;
}wrapper;

wrapper* push(wrapper*, void*, dataType);
void print(wrapper*);

int main(){
  wrapper* memptr = 0;
  int i = 10;
  char c = 'a';
  float f = 2.35f;
  double d = 2.67;
  memptr = push(memptr, &i, INT);
  memptr = push(memptr, &c, CHAR);
  memptr = push(memptr, &f, FLOAT);
  memptr = push(memptr, &d, DOUBLE);

  print(memptr);
  return 0;
}

wrapper* push(wrapper* memptr, void* vp, dataType what){
  if (size == 0){
    size++;
    memptr = (wrapper*)malloc(sizeof(wrapper) * size);
  }else{
    size++;
    memptr = (wrapper*)realloc(memptr, sizeof(wrapper) * size);
  }

  switch(what){
    case INT:
      memptr[size - 1].storage = (container*)malloc(sizeof(container));
      memptr[size - 1].storage->i = *(int*)vp; 
      memptr[size - 1].type =  INT;
      break;
    case CHAR:
      memptr[size - 1].storage = (container*)malloc(sizeof(container));
      memptr[size - 1].storage->c = *(char*)vp;
      memptr[size - 1].type =  CHAR;
      break;
    case FLOAT:
      memptr[size - 1].storage = (container*)malloc(sizeof(container));
      memptr[size - 1].storage->f = *(float*)vp; 
      memptr[size - 1].type =  FLOAT;
      break;
    case DOUBLE:
      memptr[size - 1].storage = (container*)malloc(sizeof(container));
      memptr[size - 1].storage->d = *(double*)vp;
      memptr[size - 1].type =  DOUBLE;
      break;
    default:
      printf("Error in pushing data!\n");
      exit(1);
      break;
  }
 return memptr;
}


void print(wrapper* memptr){
  int i;
  for(i = 0; i < size; i++){
    if (memptr[i].type == INT)
      printf("The data is %d\n", memptr[i].storage->i);
    else if (memptr[i].type == CHAR)
      printf("The data is %c\n", memptr[i].storage->c);
    else if (memptr[i].type == FLOAT)
      printf("The data is %f\n", memptr[i].storage->f);
    else 
      printf("The data is %lf\n", memptr[i].storage->d);
  }
}

Probably what I am asking for is that using some runtime user input in the push function. At this moment it seems to a problem because how can we give the second enum as a dataType?...Probably some version of gcc or clang may have gcc/clang specific typeof function but those are not much reliable.. I want a platform independent & version independent solution.

This article has been dead for over six months. Start a new discussion instead.