質問

I write some codes as below while the function DeleteNextNode returns a type called Position:

return (the_checked_position->next == position_to_delete) ?
     DeleteNextNode(the_checked_list, the_checked_position) :
     printf("%s\n", "No such node in the list, delete failed."), NULL;

But the syntax checher plugin give a warning :pointer/integer type mismatch in conditional expression ('Position' (aka 'struct Node *') and 'int')

So does expression's type must be same as conditional-expression in C?

My English is very poor, so you can edit my question if necessary. Thank you!

役に立ちましたか?

解決

If we look at the draft C99 standard section 6.5.15 Conditional operator paragraph 3 says:

One of the following shall hold for the second and third operands:

— both operands have arithmetic type;

— both operands have the same structure or union type;

— both operands have void type;

— both operands are pointers to qualified or unqualified versions of compatible types;

— one operand is a pointer and the other is a null pointer constant; or

— one operand is a pointer to an object or incomplete type and the other is a pointer to a qualified or unqualified version of void.

a struct Node * and int do not match any of these criteria but it does not seem like that was your intention. I see that you are using the comma operator in this expression but since the comma operator has the lowest precedence it is really effectively this:

return ( operand 1 ? operand 2 : operand 3 ) , NULL;

so instead of the result of operand 3 being NULL it is the return of printf which is int so most likely you really meant this for the third operand:

(printf("%s\n", "No such node in the list, delete failed."), NULL)

which would fit the above criteria and it probably what you originally intended. The fully corrected code would be this, with the added parenthesizes noted below the code:

return (the_checked_position->next == position_to_delete) ?
 DeleteNextNode(the_checked_list, the_checked_position) :
 ( printf("%s\n", "No such node in the list, delete failed."), NULL ) ;
 ^                                                                  ^ 
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top