-
Pointer dereference
Hi i got the following code am trying to understand:
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int *x;
int i;
x = (int *) malloc ( sizeof(int) * 100);
for(i = 0; i < 100; i++)
x[i] = i;
for(i = 0; i < 100; i++)
printf("%d ",x[i]);
printf("\n");
free (x);
for(i = 0; i < 100; i++)
printf("%d ",x[i]);
printf("\n");
}
Its the bit in red above. I thought you have to dereference to get the value so shouldnt it be:
printf("%d ",*x[i]);?
-
x[i] is equivalent to *(x + i)
Incidentally, you should not be accessing what x points to after free(x), and there is no need to cast the return value of malloc().