for (count = index, packet_no = 0;
count < TOTAL_OBJ, packet_no < TOTAL_PKT;
count++, packet_no++)
=>
逗号表达式的左操作数无效。
我发现上面的代码是正确的,无法理解为什么会出现此错误。
这是逗号运算符的工作方式,您要做的是使用OR或AND(在您的情况下,可能是AND):
// the condition for resuming the loop is that one of the conditions is true
count < TOTAL_OBJ || packet_no < TOTAL_PKT
// the condition for resuming the loop is that both conditions are true
count < TOTAL_OBJ && packet_no < TOTAL_PKT
for
语句的三个术语中的每个都有三个逗号运算符。警告是针对术语2。
术语1和3的两个表达式均按预期执行。
项2的左运算仅被评估为无效,不参与for
条件,因此会引起警告。
条件语句(用于,while或if)具有带逗号运算符的条件表达式,最后一个表达式的值是条件语句的条件值(真或假)。例如。>
int i = 1; int j = 0; int k = 1; if(i, j, k) { printf("Inside"); }else { printf("Outside"); }
以逗号运算符的形式打印“ Outside”,从左到右进行评估,k是if语句中最后一个返回假值的表达式。
int i, j; if(i = 0 , j = 1) { printf("Inside"); }else { printf("Outside"); }
上面打印“内部”。 j = 1是在if语句中计算的最后一个具有真值的表达式。
int i = 1;
int j = 0;
int k = 1;
if(i, j, k) {
printf("Inside");
}else {
printf("Outside");
}