Hello friends
Can someone explain me why
a[i]=i++ is undefined and i=i+1 is legal.
Hello friends
Can someone explain me why
a[i]=i++ is undefined and i=i+1 is legal.
The first is not actually undefined. I suspect it is unspecified (the words unspecified and undefined have different meanings).
Anyway, why don't YOU explain why you think one is undefined/unspecified and why the other is not? Then people will give you advice as to whether you are right or wrong. You will learn more that way.
This looks too much like a homework exercise, and this site has a policy that amounts to "do your own homework". So, put in the required effort, and you may get help.
No, the behaviour is undefined:Originally Posted by grumpy
Originally Posted by C++11 Clause 1.9 Paragraph 12bOf course, with assumptions about what are the types of the objects involvedOriginally Posted by C++ Clause 1.9 Paragraph 15d
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
In short, you cannot modify and use a memory location in the same expression (or unsequenced operation), except when the old value is used to compute the new value.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
I think because in first statement compiler is confused whether to store value of i++ in old a[i] or updated a[i+1].Is this the right reason.what is the difference between undefined and unspecified.
I just used this example to nerd-snipe my co-workers. it was glorious!
What can this strange device be?
When I touch it, it gives forth a sound
It's got wires that vibrate and give music
What can this thing be that I found?
I've solved the problem! It's very elegant. </sarcasm>
Code:#include <stdio.h> int main(void) { int t, i = 0; int a[10]; while (i < 10) a[t] = i++, t = i; i = 0; while (i < 10) printf("[%d]\n", a[i++]); return 0; }
O_oI've solved the problem! It's very elegant.
You've posted source that has a significant bug.
Soma
“Salem Was Wrong!” -- Pedant Necromancer
“Four isn't random!” -- Gibbering Mouther
O_oCan you give me a hint?
You use an uninitialized variable.
[Edit]
o_O
Well, I guess I can't.
[/Edit]
Soma
“Salem Was Wrong!” -- Pedant Necromancer
“Four isn't random!” -- Gibbering Mouther
O_oWould you believe I left that bug there as a test to catch the unwary?
Yes. That is obviously exactly what occurred.
Why should I believe any other thing?
Soma
“Salem Was Wrong!” -- Pedant Necromancer
“Four isn't random!” -- Gibbering Mouther