#include <stdio.h>
#include <stdlib.h>
int main()
{
char *p = malloc(sizeof(void));
p[0] = 'a';
p[1] = 'b';
p[2] = 'c';
p[3] = 'd';
p[4] = 'e';
printf("%c\n",p[0]);
printf("%c\n",p[1]);
printf("%c\n",p[2]);
printf("%c\n",p[3]);
printf("%c\n",p[4]);
return 0;
}
// In above code only one byte memory should be allocated as sizeof(void) will be only '1', but why I am able to successfully print all the characters?
similarly for below program also it is printing successfully
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *p = malloc(0);
p[0] = 'a';
p[1] = 'b';
p[2] = 'c';
p[3] = 'd';
p[4] = 'e';
printf("%c\n",p[0]);
printf("%c\n",p[1]);
printf("%c\n",p[2]);
printf("%c\n",p[3]);
printf("%c\n",p[4]);
return 0;
}
Please clarify why both of this cases are successfully printing the msg even though memory is not allocated for this?