comma-operator

sizeof taking two arguments

天涯浪子 提交于 2019-11-27 18:44:50
In C.1.3 of the C++ IS (2003. It's in the C++11 IS, too), the standard points out a difference between ISO C and C++; namely, for char arr[100]; sizeof(0, arr) returns sizeof(char*) in C, but 100 in C++. I can find no documentation for sizeof taking two arguments. The obvious fallback is the comma operator, but I don't think so: sizeof(arr) in C is 100 ; sizeof(0, arr) is sizeof(char*) . Both sizeof(0, arr) and sizeof(arr) are 100 in C++. I may be missing the whole point of the IS in this context. Can anyone help? This is similar to a question discussed back in '09, but no one referred to the

How is the comma operator being used here? [duplicate]

做~自己de王妃 提交于 2019-11-27 06:19:39
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: C++ Comma Operator Uses of C comma operator I am not new to C++, but this is the first time I see the following code: int a=0; int b=(a=2,a+1); That is C++ code. Can you tell me what is going on here? And how variable b gets value 3? 回答1: This code is equivalent to this: int a = 2 ; int b = a + 1 ; First expression to the left of comma gets evaluated, then the one to its right. The result of the right most

Why does this code produce a warning referring to the comma operator?

筅森魡賤 提交于 2019-11-27 04:46:21
问题 When answering this question, I came across this code... #include <iostream> int main() { int const income = 0; std::cout << "I'm sorry your income is: " < income; // this is line 6 } ...which contains a typo. The second (intended) << operator on line 6 has been accidentally written as a < . That aside, compiling the code using GCC 4.3.4 or 4.4.3 results in a warning: prog.cpp: In function ‘int main()’: prog.cpp:6: warning: right-hand operand of comma has no effect My question: why is that

What is the proper use of the comma operator?

我怕爱的太早我们不能终老 提交于 2019-11-27 04:44:41
I saw this code: if (cond) { perror("an error occurred"), exit(1); } Why would you do that? Why not just: if (cond) { perror("an error occurred"); exit(1); } In your example it serves no reason at all. It is on occasion useful when written as if(cond) perror("an error occured"), exit(1) ; -- then you don't need curly braces. But it's an invitation to disaster. The comma operator is to put two or more expressions in a position where the reference only allows one. In your case, there is no need to use it; in other cases, such as in a while loop, it may be useful: while (a = b, c < d) ... where

C++ — return x,y; What is the point?

南笙酒味 提交于 2019-11-27 04:33:14
问题 I have been programming in C and C++ for a few years and now I'm just now taking a college course in it and our book had a function like this for an example: int foo(){ int x=0; int y=20; return x,y; //y is always returned } I have never seen such syntax. In fact, I have never seen the , operator used outside of parameter lists. If y is always returned though, then what is the point? Is there a case where a return statement would need to be created like this? (Also, I tagged C as well because

How do I put two increment statements in a C++ 'for' loop?

▼魔方 西西 提交于 2019-11-27 00:01:36
I would like to increment two variables in a for -loop condition instead of one. So something like: for (int i = 0; i != 5; ++i and ++j) do_something(i, j); What is the syntax for this? A common idiom is to use the comma operator which evaluates both operands, and returns the second operand. Thus: for(int i = 0; i != 5; ++i,++j) do_something(i,j); But is it really a comma operator? Now having wrote that, a commenter suggested it was actually some special syntactic sugar in the for statement, and not a comma operator at all. I checked that in GCC as follows: int i=0; int a=5; int x=0; for(i; i

How does the compiler know that the comma in a function call is not a comma operator?

回眸只為那壹抹淺笑 提交于 2019-11-26 23:11:50
问题 Consider the function call (calling int sum(int, int) ) printf("%d", sum(a,b)); How does the compiler decide that the , used in the function call sum(int, int) is not a comma operator? NOTE : I didn't want to actually use the comma operator in the function call. I just wanted to know how the compiler knows that it is not a comma operator. 回答1: Look at the grammar for the C language. It's listed, in full, in Appendix A of the standard. The way it works is that you can step through each token

return list of values between parenthesis (10, 20, 30, 40)?

丶灬走出姿态 提交于 2019-11-26 21:42:03
问题 I am working in C++ (not C++/CLI) in Visual Studio 2012 . I don't understand why this code works, I would have expected it to fail at compilation time, but it doesn't even fail at runtime: double MyClass::MyMethod() const { //some code here return (10, 20, 30, 40); } I produced this code by mistake, wasn't on purpose, I noticed the mistake when I was running my Unit Tests. And I am surprised it works. When I run it, it returns 40 , the last number on the list. Can someone explain me what this

What does the comma operator do?

梦想与她 提交于 2019-11-26 20:19:04
问题 What does the following code do in C/C++? if (blah(), 5) { //do something } 回答1: Comma operator is applied and the value 5 is used to determine the conditional's true/false. It will execute blah() and get something back (presumably), then the comma operator is employed and 5 will be the only thing that is used to determine the true/false value for the expression. Note that the , operator could be overloaded for the return type of the blah() function (which wasn't specified), making the result

What is the proper use of the comma operator?

夙愿已清 提交于 2019-11-26 11:21:11
问题 I saw this code: if (cond) { perror(\"an error occurred\"), exit(1); } Why would you do that? Why not just: if (cond) { perror(\"an error occurred\"); exit(1); } 回答1: In your example it serves no reason at all. It is on occasion useful when written as if(cond) perror("an error occured"), exit(1) ; -- then you don't need curly braces. But it's an invitation to disaster. The comma operator is to put two or more expressions in a position where the reference only allows one. In your case, there