New to C here, I am studying pointers. So my question "Are the bytes allocated by variables consecutive in memory?" is because I thought that by creating a char pointer from an integer pointer (char *p_c = p_i - (sizeof(char)))
, the char pointer would point exactly to the second byte of the integer.
E.g.: An integer variable takes 4 bytes and a char variable takes 1 byte.
#include <stdio.h>
int main(){
int a = 4276545;
int *p = &a;
char *p_c1 = p - (1*sizeof(char));
printf("Value of p_c1 (second byte of a) is %c\n", *p_c1);
}
The ASCII 'A' char in binary is 01000001. 4276545 decimal to binary is 00000000 01000001 01000001 01000001.
Isn't p_c1 pointing to 00000000 01000001 01000001 01000001?
Printing *p_c1, I was expecting it to be 'A', but it prints 'P'. And it's not as it was a random char which always changes in each execution, it always prints 'P'. Why?