Hello ladies and gents,

Ive got an exercise in which I have to fully parenthesize expressions using the higher-lower precedence, for instance:

a+b*c would become (a+(b*c)) because multiplication has a higher precedence then addition.

Here are my solutions for the following expressions:

1) a = b + c * d << 2 & 8
(a = ((b + (c * d)) << 2 & 8))

2) a & 077 != 3
Don't think there is a solution for this since & is "adress of", but how can you have an adress of 077?

3) a == b || a == c && c < 5
(((a == b) || ((a == c) && (c < 5))))

4) c = x != 0
(c = (x != 0))

5) 0 <= i < 7
((0 <= i) < 7)

6) f(1, 2) + 3
((f(1, 2)) + 3)

7) a = - 1 + + b -- - 5
Don't think there is a solution either since the expressions are not correct?

8) a = b == c ++
(a = (b == (c ++)))

9) a = b = c = 0
(a = (b = (c = 0)))

10) a[4][2] *= * b ? c : * d * 2
((a[4][2]) *= (* b ? c : ((* d) * 2)))

11) a-b, c=d
((a-b), (c=d))

I was hoping that you guys could check these out and tell me which ones are wrong?

Thank you.

10 is not a valid statement. you can write a short program that tests each of your problems.

int main(int argc, char* argv[])
{
	int a[5][3] = {0};
	int b = 1;
	int c = 2;
	int d = 3;

	a[4][2] *= * b ? c : * d * 2
	return 0;
}

:\dvlp\test3\test3.cpp(15) : error C2100: illegal indirection
d:\dvlp\test3\test3.cpp(15) : error C2100: illegal indirection

it could be correct if b and d were both of type int* - but since the OP's assignment doesn't say otherwise, a safe assumption is that these are just plain old numbers (or perhaps literals)

Hello ladies and gents,

Hi! :)

1) a = b + c * d << 2 & 8
(a = ((b + (c * d)) << 2 & 8))

You missed some parentheses. << and & have different precedence.

2) a & 077 != 3
Don't think there is a solution for this since & is "adress of", but how can you have an adress of 077?

& is the bitwise AND operator, so the expression is valid and can be parenthesised.

7) a = - 1 + + b -- - 5
Don't think there is a solution either since the expressions are not correct?

That's a cruel example. It's a valid expression, but whomever wrote it was trying to be mean and trick you with whitespace. The first - makes 1 negative, the second + is a unary operator that makes b nonnegative and the -- applies to b. If you use more sensible spacing, it looks like a = -1 + +b-- - 5 .

10) a[4][2] *= * b ? c : * d * 2
((a[4][2]) *= (* b ? c : ((* d) * 2)))

That's a little off. ;)

commented: Thanks for the tips +3

10 is not a valid statement. you can write a short program that tests each of your problems.

Ok, I'll do that AD :!:

Hi! :)

Hi Ravalon ;)

You missed some parentheses. << and & have different precedence.
& is the bitwise AND operator, so the expression is valid and can be parenthesised.

Darn :cheesy:

That's a cruel example. It's a valid expression, but whomever wrote it was trying to be mean and trick you with whitespace. The first - makes 1 negative, the second + is a unary operator that makes b nonnegative and the -- applies to b. If you use more sensible spacing, it looks like a = -1 + +b-- - 5 .

Well, you know Bjarne, cheeky fella :cheesy:

That's a little off. ;)

Darn Darn :cheesy:

it could be correct if b and d were both of type int* - but since the OP's assignment doesn't say otherwise, a safe assumption is that these are just plain old numbers (or perhaps literals)

Yep, tried that and it worked, but, as you say, there is no explanation as to what the variables represent, pointers or numbers.

I don't know Mr. Jobe if you already know this but there is a book which presents the solutions to the problems posed in C++ Programming Language, you can get it here.

And if you think the book is too expensive or not worth it you can always download the source code for free from here.

Hope it helped, bye.

I don't know Mr. Jobe if you already know this but there is a book which presents the solutions to the problems posed in C++ Programming Language

Nope, didn't know that s.o.s, thanks for that.

I'd appreciate it if you didn't call me Mr. Jobe, makes me feel old and superior somehow, either JoBe or Johan(my real name) will do ;)

And if you think the book is too expensive or not worth it...

I certainly think it's worth it, but, due to lack of trust in online buying with a credit card, I tend to use a store called proxis.be and they don't sell it anymore.

Anyway, thanks for that, I'll surely see if I can't buy it anywhere else :!:

I'd appreciate it if you didn't call me Mr. Jobe, makes me feel old and superior somehow, either JoBe or Johan(my real name) will do ;)

Good luck getting Mr. ~s.o.s~ to do that :mrgreen: He is a very respectful person -- to a fault...

I certainly think it's worth it, but, due to lack of trust in online buying with a credit card, I tend to use a store called proxis.be and they don't sell it anymore.

Interesting.... And after all these years, too. Out of curiosity, when you go to a restaurant, when the pink & orange spike haired girl with tattoos and piercings asks for payment, do you hand your credit card to her and she disappears with it? ;)

Interesting.... And after all these years, too. Out of curiosity, when you go to a restaurant, when the pink & orange spike haired girl with tattoos and piercings asks for payment, do you hand your credit card to her and she disappears with it? ;)

Pink & orange spiked hair with tattoos...? What sort of restaurants do you visit? ;)

I'd appreciate it if you didn't call me Mr. Jobe, makes me feel old and superior somehow, either JoBe or Johan(my real name) will do ;)

Don't know why all foreigners dislike being addressed as Mister. This is what we are taught in our country to give utmost respect to everyone.;)

Saying Mister is not an indication of you going old or being superior to someone. For me its always being "what goes around comes around".

But still if you don't like it, I will try to refrain from saying it the next time, though there are no guarantees...;)

Don't know why all foreigners dislike being addressed as Mister. This is what we are taught in our country to give utmost respect to everyone.;)

Saying Mister is not an indication of you going old or being superior to someone. For me its always being "what goes around comes around".

But still if you don't like it, I will try to refrain from saying it the next time, though there are no guarantees...;)

Well, I guess kids these days don't get taught any respect! ;)

Actually, its used the same way over here - although usually only on really formal occasions (Such as in a courtroom, an interview, or in a formal letter). most of the time we're all just too lazy for all that!

Interesting.... And after all these years, too. Out of curiosity, when you go to a restaurant, when the pink & orange spike haired girl with tattoos and piercings asks for payment, do you hand your credit card to her and she disappears with it? ;)

Well, I actually don't have a credit card :cheesy: And yeah, like Bench said, in what kind of restaurants do you go and eat :lol:

Don't know why all foreigners dislike being addressed as Mister. This is what we are taught in our country to give utmost respect to everyone.

It's not that I dislike it, on the contrary, it shows that your parents brought you up in a very descent manner and we to are thought to be curtious and polite, just, .... not on a forum like this :cheesy:

But still if you don't like it, I will try to refrain from saying it the next time, though there are no guarantees...

Thanks s.o.s, appreciated ;)

Well, I guess kids these days don't get taught any respect!

LOL :lol: Never thought that at the age of 37, I would be called a KID LOL :lol: Thanks .... Mr. Bench ;)

Don't know why all foreigners dislike being addressed as Mister. This is what we are taught in our country to give utmost respect to everyone.;)

Being called Mr. Raye is awkward on many levels, but I can ignore all of them except where it feels like you're just dealing with a stranger rather than trying to make a friend. ;) Frankly, there's a difference between respect and professional coldness.

I don't mind being addressed as Mister, but eventually I'll see you as enough of a mate to be bothered by it. At that point I'll ask you to use a more familiar means of address. :)

Hello.

.... you're just dealing with a stranger rather than trying to make a friend. ;) Frankly, there's a difference between respect and professional coldness.

I guess different cultures have different ways of interpreting different things. I was never before given the impression that Mister amounts to professional coldness or a complete stranger -- and it has been a while since I was born.

So in the end I guess I really am a cold hearted, unfriendly person.....

Thank you.

I don't mind being addressed as Mister, but eventually I'll see you as enough of a mate to be bothered by it. At that point I'll ask you to use a more familiar means of address. :)

Don't worry, he will eventually. Just look at me, now instead of calling me "Mr. Joe" he now calls me Joey!

And no I don't really think that a Mr. prefix is bad at all, especially with new members. It really shows respect.

Don't worry, he will eventually. Just look at me, now instead of calling me "Mr. Joe" he now calls me Joey!

And no I don't really think that a Mr. prefix is bad at all, especially with new members. It really shows respect.

Oh, don't misunderstand me, it's definitely not bad. But one man's respect is another's weirdness. I was raised to be polite, but I think I'm still too young to be comfortable with formal titles in everyday conversation. ;)

Interesting.... And after all these years, too. Out of curiosity, when you go to a restaurant, when the pink & orange spike haired girl with tattoos and piercings asks for payment, do you hand your credit card to her and she disappears with it? ;)

Pink & orange spiked hair with tattoos...? What sort of restaurants do you visit? ;)

All kinds. Around the U you get more of these with the strange dress code...

Hello ladies and gents,

Ive got a few more expressions I needed to fully parenthesize:

1) *p++ becomes *(p++)

2) *--p becomes nothing
if *--p would have been --*p:
--*p OR--(*p) would have the same result.
Reason is, both would have the same precedence, so, it
goes from left to right in both cases.

3) ++a-- becomes ++(a--)

4) (int*)p->m becomes (int*)(p->m)

5) *p.m becomes *(p.m)

6) *a becomes *(a)


One more question, am I correct that the following expression:
(int*)p->m equals a pointer member which get's a type cast conversion?

3) ++a-- becomes ++(a--)

Undefined. See this

Hello ladies and gents,

Ive got a few more expressions I needed to fully parenthesize:

1) *p++ becomes *(p++)

2) *--p becomes nothing
if *--p would have been --*p:
--*p OR--(*p) would have the same result.
Reason is, both would have the same precedence, so, it
goes from left to right in both cases.

3) ++a-- becomes ++(a--)

4) (int*)p->m becomes (int*)(p->m)

5) *p.m becomes *(p.m)

6) *a becomes *(a)


One more question, am I correct that the following expression:
(int*)p->m equals a pointer member which get's a type cast conversion?

Forgive me, but without declarations and the intent of the operations, there is really not a question being asked here.

And this one ++a-- seems to be a syntax error. Both prefix and postfix result in values, so no matter which you choose you end up doing something akin to ++42 .

Forgive me, but without declarations and the intent of the operations, there is really not a question being asked here.

Hi Dave, I understand what you're saying, but the examples that I gave are how they are given in the exercise for the Bjarne Stroustrup The C++ Programming Language, there isn't anything mentioned about declarations or intent of operations.

I guess it's just an exercise to practise your knowledge of order of precedence for the various expressions.

@ Walt, thanks for that Walt, but, as said previous, don't think this exercise means much more then to see whether you know what expression has a higher precedence.

Thanks for the help guys ;)

Hi Dave, I understand what you're saying, but the examples that I gave are how they are given in the exercise for the Bjarne Stroustrup The C++ Programming Language, there isn't anything mentioned about declarations or intent of operations.

Ah. Hm. An exercise lacking a greater purpose I guess.

[rant=slight]
Although one of Brian Kernighan's tips is to "[p]arenthesize to avoid ambiguity", I generally prefer to use simpler expressions. If I have to think about an expression for a "fair amount of time" (around a minute) it is a candidate for simplification -- not parentheses (I find they can add further confusion to an already difficult expression). [It'll be a maintenance nightmare if the original coder has stare at his own code for too long to see what's going on.]

Why does it seem to me that the buggiest code I've ever worked with was over-parenthesized? Casts and parenthetical groupings seem to be where the hardest to find bugs live, so I avoid casts and paren groupings.

I'll coin this one: if you can't comment it, don't code it. In light of that, if you can't explain a need for the added parentheses in comments, don't add them to the code.
[/rant, sorry]

That's a very good rant.... Totally agree.

<offtopic>

One more controversial rant in the C++ commnunity:

He who fails to design for performance, performs a design failure.

Many people misunderstand the quote by Knuth

Premature optimization is the root of all evil

as there is not need for optimization. They fail to see that optimzations in the form of simple logical things for eg. using char* instead of strings whenever applicable which may prevent the software from taking a major performance hit but might cause the supporters of Abstractomania to grasp....

</offtopic>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.