How to access value allocated by malloc
My code is not working as it supposed to do. I want a program that allocates memory for three integer's, then I want to assign value at each memory location, then I want to print value stoared at each location's
Code:
#include<stdio.h>
#include<stdlib.h>
int main ()
{
int i;
int *ptr;
ptr = malloc( 2 * sizeof(*ptr));
if( ptr != NULL ) /*Check for failure. */
{
*ptr = 0;
ptr++;
*ptr = 1;
ptr++;
*ptr = 2;
ptr++;
for ( i = 0; i < 2; i++)
{
printf("%d \n", *ptr);
ptr++;
}
}
free( ptr ); /*Release memory. */
return 0;
}
I don't get output as I want.
When I compile code I get this output
234918894
7274542