Consider the following C program:
#include <stdio.h>
#include <stdlib.h>
int main(){
char c[1] = {'Q'};
printf("%c ",*(char*)(c)); // line 1
printf("%c\n",*(char*)(&c)); // line 2
}
the output is Q Q
Here is my understanding of what should happen, c is a pointer to a char, so the char printed by line 1 should be the letter Q because the pointer to a char is being cast as a pointer to a char (so nothing happens) then it is dereferenced. Because the char that c points to is 'Q', line 1 prints 'Q'. This seems to make sense to me.
However line 2 does not. The address of c is cast as a pointer to a char so what I believe should happens is after being dereferenced the expression *(char*)(&c) should simplify to the value of the pointer c but expressed as a char.
These both give the same result and I don't think it is a coincidence because I've tried it on many different letters. I'd like to know why this is. Thanks
PS:
I tried this:
#include <stdio.h>
#include <stdlib.h>
int main(){
char c[10] = "asdf";
printf("%c ",*(char*)(c)); // line 1
printf("%c\n",*(char*)(&c)); // line 2
}
and I got this: a a