The following is one way to write your code:
Code:
#include<stdio.h>
#include<stdlib.h>
int main ()
{
int *x = malloc(5 * sizeof(*x)); // 5 * sizeof(int) NOT sizeof(int *)
// NEVER change the address that malloc() returns!!! If you change that value,
// then when you free(x), you will corrupt the heap!!!
int i = 0;
for ( i = 0; i < 5; i++)
{
x[i] = i; // Set the values of the array, 0 - 4
printf( "Value of x[%d] = %d\n", i, i);
}
printf("\n");
for ( i = 0; i < 5; i++)
{ // Cast the address x to a void * to correctly print the address
printf("Value %d store at address %p\n", x[i] , (void *) (x + i));
}
free(x); // Free the memory allocated!
return 0;
}
I would also use a better name for the variable x. Single letters are usually used as the loop variable for foo(), etc...