Comma in CPP

Started by
33 comments, last by DaBookshah 17 years, 5 months ago
Quote:Original post by M2tM
Do brackets enable this to be safe?
i = (i++);


Nope. You need to add a sequence point. Brackets are used to make priority explicit, but in this case the priority would have been the same implicitly anyway. In this case, the value of i++ is evaluated before i = * anyway, but the side-effect of i++ need not be performed to get the value of i++ (in the case of ++i, this would have worked).

Quote:*edit: It seems it goes based on something called "sequence points" I'll look that up, but feel free to supply a description or links.


That's pretty simple: if two expressions are separated by a sequence point, the expression before it is entirely executed (along with its side-effects) before the expression after.
Advertisement
Thanks for the response. It's learning about little things like this each day which humble you and make you realize how much you have yet to learn. The devil's in the details. Your time is appreciated.

*edit: Shoots, I'm sorry ToohrVyk. I tried re-rating you because I thought I had rated you a long time ago when I was at like 1100 hoping it would bump your rating one or two points but it dropped one :(
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk
to go back to this:

i = 1;
i = i++;

it executes like:
i = i;
i++; //i = 2

if you want to do it the other way, use:

i = ++i;

this does:
i++;
i = i; //i is also 2 after that


i = (i++);
works just fine, like the second example.
you can do this.
it does (when using a temp value):

temp = i++;
i = temp;
Quote:Original post by Anonymous Poster
i = (i++);
works just fine, like the second example.


This does two things:
A- set i to its initial value
B- add 1 to i

Since there is only one sequence point, the order of execution may be either AB or BA.

AB adds 1 to i.
BA does not change i.

Therefore, it does not "work just fine". Except if by "works just fine" you meant "causes undefined behaviour".
Quote:Original post by The C modest god
I am just writting a small CPP like compiler.


Yikes, why?

Quote:Original post by ToohrVyk
(in the case of ++i, this would have worked).

It almost certainly would have worked, but the behavior is still undefined.

CM
Quote:Original post by The C modest god
What is the difference between:
[...]

None, in both cases you hate yourself and your future readers. There might be justifiable uses of the comma operator, but I cannot think of one.

Omae Wa Mou Shindeiru

Quote:Original post by M2tM
*edit: Shoots, I'm sorry ToohrVyk. I tried re-rating you because I thought I had rated you a long time ago when I was at like 1100 hoping it would bump your rating one or two points but it dropped one :(


I fixed it. [wink] Not sure why I hadn't rated him yet though.
Quote:Original post by DaBookshah
Quote:Original post by The C modest god
I am just writting a small CPP like compiler.


Yikes, why?


I want to remove some of the game CPP code into my script code.
Also, I want a future possiblity for players to add content to my games.
And maybe also a possiblity for programs to program themself.

It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
Quote:Original post by tstrimp
Quote:Original post by M2tM
*edit: Shoots, I'm sorry ToohrVyk. I tried re-rating you because I thought I had rated you a long time ago when I was at like 1100 hoping it would bump your rating one or two points but it dropped one :(


I fixed it. [wink] Not sure why I hadn't rated him yet though.

I suppose you didnt think to rate me up? Geez.
I am just joking. I am the... god dammit, how did that boxer called him self when he was sent to jail?
Anyway, I have simply stopped wanting to be rated up instead of trying to get rated up.

It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.

This topic is closed to new replies.

Advertisement