getting values from uncasted pointers in c
#include "stdio.h"
#include "stdlib.h"
//generic macros for accessing casted pointer types
//macro for Base
#define BASE_HANDLE int type; \
struct Base* next; \
struct Base* prev;
struct Base {
BASE_HANDLE
};
typedef struct Base Base;
//object that represents integer.
typedef struct {
BASE_HANDLE
long value;
} IntObj;
//constructs new int casted back to base
Base* IntObj_new(long value)
{
IntObj* iob = malloc(sizeof(IntObj));
iob->type = 1;
iob->value = value;
return (Base*)iob;
}
#define IntObj_GET_INT(bs) ((IntObj*)bs)->value
int main(void) {
Base* foo = IntObj_new(7);
printf("The number is %d\n", IntObj_GET_INT(foo));
return 0;
}