Hello !
I've been testing some stuff with pointers.
Here is one of them :
I tried to check if the output will be an error because I've only reserved 1 byte, but when I wrote 2562323 into this one byte it saved and printed this value. How ??? What if in next byte after the malloc there is something ?
Same went when I changed malloc from int to char
I expected that the only number that will appear is 2. Because the memory is for char, so only 1 byte, so number "2" at the beggining will be saved in this one byte and the rest will be ignored Does this "type" infront of malloc matter ?
I've been testing some stuff with pointers.
Here is one of them :
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *matrix = (int *)malloc(1);
*(matrix) = 2562323;
printf("%d", *matrix);
return 0;
}
I tried to check if the output will be an error because I've only reserved 1 byte, but when I wrote 2562323 into this one byte it saved and printed this value. How ??? What if in next byte after the malloc there is something ?
Same went when I changed malloc from int to char
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *matrix = (char *)malloc(1);
*(matrix) = 2562323;
printf("%d", *matrix);
return 0;
}
I expected that the only number that will appear is 2. Because the memory is for char, so only 1 byte, so number "2" at the beggining will be saved in this one byte and the rest will be ignored Does this "type" infront of malloc matter ?
Last edited: