I'm building a tiny linked list library for the purposes of self-enrichment, which has forced me to deal with a problem I'm nearly at a loss to explain. Take the following example, and assume that new_item->data
is a void pointer inside a typedef struct:
int *data = malloc(sizeof(int));
*data = 42;
MY_LIST *new_item = malloc(sizeof(*new_item));
new_item->data = &data;
// prints what looks like a memory address
printf(
"data: %i\n",
*((int *) new_item->data)
);
//prints 42
int data2 = *((int *) new_item->data);
printf(
"data2: %i\n",
*((int *) data2)
);
As far as my (probably derelict) brain can tell, in both cases I'm using the same cast statement - *((int *) new_item->data)
- to derive the integer from its container. Yet when I use it inline inside of printf, I get a memory address instead. What am I misunderstanding about this?