sandeep
sandeep

Reputation: 11

What causes a compiler error while doing a bitwise &?

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 reason for this error?

Upvotes: 0

Views: 165

Answers (3)

Frank
Frank

Reputation: 211

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.

Upvotes: 5

EmeryBerger
EmeryBerger

Reputation: 3975

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;

Upvotes: 3

Cipher
Cipher

Reputation: 6102

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.

Upvotes: -1

Related Questions