Boolean expression in C

I found this expression in a C program, and I did not understand it:

struct stack_rec *ss; ss=(struct stack_rec *)EMalloc(sizeof(struct stack_rec)); if (ss) { int res; res = (ss->elem = * i , 1); // what does this mean ???? if (res <= 0) return res; if (*s == 0) { ss->next = 0; } else { ss->next = *s; } *s = ss; return 2; } return 0; 

What does res = (ss->elem = * i , 1); mean res = (ss->elem = * i , 1); ? Is this a logical expression? I tried it with 0 instead of 1, and it always returns the value of the second parameter! Can someone explain this expression please?

+4
source share
2 answers

Hacks This is the use of a comma operator, which simply evaluates the value of the final expression, i.e. 1 .

Therefore, since this code is equivalent:

 ss->elem = *i; res = 1; 

Subsequent res testing seems pointless and thus broken.

+9
source

The comma is not a very used C operator.

Basically, what he does is execute 2 statements (ss-> elem = * i; and 1;). Statement 1; don't do much.

After that, it returns the result from the last statement (in this case 1)

+1
source

All Articles