I don't know if I am doing something wrong or if my concept is somewhat wrong
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *p;
p=calloc(3,sizeof(int));
p[0]=10;
p[1]=15;
p[2]=30;
printf("\n%d\n%p\n%d\n%p\n%d\n%p\n\n",p[0],p,p[1],p+1,p[2],p+2);
free(p);
//p=NULL;
printf("\n%d\n%p\n%d\n%p\n%d\n%p\n\n",p[0],p,p[1],p+1,p[2],p+2);
return 0;
}
When the 2nd, printf() is run, it shows p[2]=30, whereas p[0]=p[1]=0 (in gcc ubuntu and some arbitary values in Code::Blocks windows). I have 2 questions.
- Why free() frees first 2 pointers and not the 3rd. one?
- Why are the values shown 0 in ubuntu when it seems right to show arbitary values?
I am a beginner so please bear with me. I have tried the same thing with malloc(), and same thing happens.