Hello,
i have 2 small doubts regarding pointers.
1)im trying to defererence 4 bytes as shown, and im initializiing each byte using a char pointer. But im getting runtime errors.
Code
char a = 65;
char *ptr = (char *)malloc(4);
int *ptr1;
ptr = &a;
ptr1 = &a;
*ptr = 0;
*(ptr + 1) = 0;
*(ptr + 2) = 0;
*(ptr + 3) = 1;
printf("\n%d", *ptr1); // dereferencing 4 bytes here.
}
2)
char a = 65;
char *ptr = (char *)malloc(4);
int *ptr1;
ptr = &a;
ptr1 = &a;
*ptr = 0;
*(ptr + 1) = 0;
*(ptr + 2) = 0;
*(ptr + 3) = 1;
printf("\n%d", *ptr1); // dereferencing 4 bytes here.
Now, i have not allocated memory for the char pointer but im trying to access subsequent addresses. But it did not give me runtime errors.
Can anyone please explain these two cases?
Thanks.