So I am learning dynamic memory allocation nowadays and stuck in a problem(if it is).
int *p;
p=(int*)calloc(5,sizeof(int));
According to my intution p should point to memory location of 5*4 = 20byte and these 20bytes only belong to p. for *(p+0), (p+1)....(p+4) everything is fine. but when i do *(p+5) or *(p+12)=100; This should give me an error as i have exceeded the memory block allocated to *p.
The confusion is : Why we need to even allocate memory when we can just do like :
int *p
int s[]={1,2,3}
p=&s[0]
//and then make an array of length > s
*(p+4)=....something
In every article i am seeing they are allocating memory before assigning . what's the need. I hope u understand my confusion.