I have a char pointer pointing to a char:
char *a = 'A';
And while doing a bitwise &
:
*a & 0x11
I am getting a compilation error. What could be the re开发者_JAVA技巧ason for this error?
a
is a variable pointing to the character at memory location 65. Operating systems usually do not allow access to such addresses and give you a segmention violation.
If you declare a
not as a pointer, then it works.
char a = 'A';
char b = a & 0x11;
printf ("%x %x\n", a, b);
Still, the result depends on the signedness of char
and the used character set.
You are incorrectly storing a character ('A'
, single quotes) into a pointer to char. You can fix this by storing a pointer to a string ("A"
, double quotes) though in this case, you will also need to add const
since those strings are constants.
const char *a = "A";
char v = (*a) & 0x11;
The way it's done is
char i='A';
char *a = i;
or
char i='A';
char *a;
a=&i
Pointer can only store the address.
精彩评论