Perhaps you would like to take a good look at the code below and observe the output:
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int x = 5;
int *ptr;
printf("&x = %p\n", (void*)&x);
ptr = &x;
printf("ptr = %p\n", (void*)ptr);
ptr = malloc(sizeof(*ptr));
*ptr = x;
printf("ptr = %p\n", (void*)ptr);
free(ptr);
return 0;
}
Note that pointers are printed with the %p format specifier, and should be pointers to void when printed, hence the cast.
EDIT:
Oops, I noticed a mistake: dynamic memory allocation is in fact needed, so the mistake is not with free(), but the lack of malloc().