If I create a void pointer, and malloc a section of memory to that void pointer, how can I print out the individual bits that I just allocated?
For example:
void* p = malloc(24);
allocates 24 bytes and stores the address of first byte in p
. If you try to print the value of p
, you'll be actually printing the address. To print the value your pointer points to, you need to dereference it by using *
. Also try to avoid void
pointers if possible:
unsigned char *p = malloc(24);
// store something to the memory where p points to...
for(int i = 0; i < 24; ++i)
printf("%02X", *(p + i));
And don't forget to free
the memory that has been allocated by malloc
:)
This question could help you too: What does "dereferencing" a pointer mean?