In the course of some questions here in the OS I saw this example and I had doubts.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
clrscr();
int *ptr,*temp;
int i;
ptr = (int *)malloc(4*sizeof(int));
temp = ptr;
for(i=0;i < 4;i++)
{
printf("Enter the Number %d : ",i);
scanf("%d",ptr);
ptr++;
}
ptr = temp;
for(i=0;i < 4;i++)
{
printf("\nNumber(%d) : %d",i,*ptr);
ptr++;
}
getch();
}
What is really happening in this example?
Why do you use ptr++
and not ptr[x]
?
Does malloc not always allocate continuous memory?