void pointers

okay how would i go about solving this question i have written up a small piece of code that should do most of it but i cant figure out how to finish off the last part.

here is the question: You are given a void pointer to some dynamically allocated memory. You know it contains ints and there are 50 of them. How do you print each int to the screen?

and before you ask this is a uni task but i am at a loss to how to finish it any ideas would be handy this is what i am using for the problem this should show everything i think but i am not sure =D




#include <stdio.h>
#include <stdlib.h>



int main()



{
void* iInts = (int*)malloc(sizeof(int) * 50);

{
for(int i = 0; i < 50; i++)
printf("%i \n", iInts[i]);
}

}
You can't dereference a void*. What you CAN do, is cast a void* to some other pointer type.
Topic archived. No new replies allowed.