|
-
March 3rd, 2003, 04:44 AM
#1
little question
Dear Friends
I like to know what will be the o/p of the following code
..........
a[i++] = 3;
.......
will the value of 3 goes in i before incrementing or after incrementing.
also another question is...........
is there is any difference in comma operator " , " when applied to c and when applied to c++
i got different answer in c and c++
Thanks in advance
vishal
-
March 3rd, 2003, 05:15 AM
#2
i will be incremented after.
if i=2 for example. a[2]=3 is the result and then i will become 3;
-
March 3rd, 2003, 05:18 AM
#3
is equivalent to
Code:
a.operator[](i++) = 3;
As you know, the function arguments are fully evaluated before the function is entered, so i++ will be evaluated before assigning 3 to the array element. However, the "return value" of i++ is the previous value of i (because of the postincrement).
So, 3 goes to the i-th element, and i is incremented "afterwards". If a is an array of built-ins, or of objects that have a natural semantics of operator[], your code is equivalent to (in the absence of exceptions):
If the operator[] has side effects, or throws an exception, the code snips are not equivalent.
I am not aware of any difference regarding the comma operator in C vs. C++, but I didn't think of this question in detail.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|