I am new to dealing with pointers and memory allocation. I don't understand what am I doing wrong. This is my code:
#include <stdio.h>
#include <stdlib.h>
int main() {
int* i;
printf("Size of i:%d\n",sizeof(i));
i = malloc(5 * sizeof(int));
printf("Size of i:%d\n",sizeof(i));
i[100] = 3;
printf("Impossible i:%d\n",i[100]);
}
I compile it using gcc file.c
. From my understanding, this should not have been compiled since i[100]
is not allocated. Instead I get an output:
Size of i:8
Size of i:8
Impossible i:3
What am I missing here? Disregarding the impossible i
, to my understanding the output of the first two lines should have been
Size of i:8
Size of i:40