AND and AND and adress of

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok. So I was going thrue the ansi C specification and somthing struck me about the &var, a&b and && opperators. Acording to the order of priorety the & operators have highter prio then the && operator so
a & b && c is the same as (a & b) && c. But if this is true sholdent
a&&b be interpreted as a&(&b) a bitwizeand address of b as space caracters are optional in the places they may accure in C? AKA a&&b == a & &b :shock:
Or have I just missread and or missunderstood the whole thing? :oops:

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A space may not occur in the logical and operator &&. The form a&(&b) results in the error "invalid operands to binary &".

Jim

Your message here - reasonable rates.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
sholdent
a&&b be interpreted as a&(&b) a bitwizeand address of b

I'm not a compiler guru but I guess that when the compiler sees a & it goes looking for another & before deciding if it is a & or a &&.

Quote:
as space caracters are optional in the places they may accure in C?

Not true, do an experiment, 'if (x & & y){}', gave compiler error 'invalid operands to binary &', so I suppose the compiler, when looking for 'tokens', specifically looks for "&" or "&&". " " is ignored, yes, outside the 'token', but that doesn't mean you can have a space between "&&". If you have a variable called "counter" you can't spell it "cou nter".

PS Use better grammar and you will get more responses.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
PS Use better grammar and you will get more responses.

Give him a break! He's from Sweden. (Although as I might have mentioned elsewhere, Scandinavians tend to be better at English than a lot of natives).

Quebracho seems to be the hardest wood.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The lexical analyzer (generated by flex in the case of gcc) will try to build the longest valid token it can before giving it to the parser. So, '&&' will be returned as a single token, '&&' will be returned as two '&' tokens. The parser never sees the white space, the lexer strips it all out. The parser (generated by bison in the case of gcc) uses operator precedence to disambiguate between infix '&', as in a&b, and prefix '&', as in taking an address: &thing.

I was surprise the compiler whined about a&(&b). Seems like a reasonable thing to do to me. Maybe you have to cast it to an int to make the compiler happy? a&((int)&b)

-dave

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Also, since you seem to be just starting to climb the learning curve on C operators, be aware that & and && are two very different things. '&' does computation. '&&' is short hand notation for an 'if' statement. '&&' and '||' are called "short crcuit" operators.

0 & exp ; // expression is evaluated, bit-wise and'ed with 0, result is zero.
0 && exp ; // 0 is tested, and found 'false', so exp is *guaranteed* *not* to be evaluated. The generated code contains a branch.

pointer!=NULL && pointer->stuff == testvalue; // good

pointer!= NULL & pointer->stuff == testvalue; // very, very bad.

-dave

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

dbc wrote:

I was surprise the compiler whined about a&(&b). Seems like a reasonable thing to do to me. Maybe you have to cast it to an int to make the compiler happy? a&((int)&b)

-dave

Yes, the cast works, but I was surprised to get an error rather than a warning about incompatible types.

Jim

Your message here - reasonable rates.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Give him a break! He's from Sweden.

:shock:

Quote:

(Although as I might have mentioned elsewhere, Scandinavians tend to be better at English than a lot of natives)

Jonn! Att furst i gott angri bat wenn i raid jor rityng in thu parent isis i fell more hapi.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Yes, the cast works, but I was surprised to get an error rather than a warning about incompatible types.

Yes, the message doesn't give much of a hint as to what is going on. But I'm not going to throw stones -- getting good error messages for expression evaluation out of a C compiler is a tough job.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

JohanEkdahl wrote:
[
Quote:

(Although as I might have mentioned elsewhere, Scandinavians tend to be better at English than a lot of natives)

Jonn! Att furst i gott angri bat wenn i raid jor rityng in thu parent isis i fell more hapi.

:lol:
Juhun! Yuoo hefe-a mestered zee Ingleesh lungooege-a perffectly! I boo doon tu yuoor mestery! Bork, bork, bork!