The code gives an error saying that “no operator matches these two operands” in the if comparison statement. I interpret,it should mean that “a node can’t be converted/casted into an integer”. But, the print statement prints an integer value for w[2] when used with %d format. Why is that happening? Isn’t printf casting it?
NODE *w=(NODE *)malloc(4*sizeof(NODE));
if(w[2]==0)
printf("%d\n",w[2]);
The structure of the node is-
struct node{
int key;
struct node *father;
struct node *child[S];
int *ss;
int current;
};
Please refer to the comments of cdhowie . He has answered the question.
The behavior you are seeing with respect to
printf()is undefined.printf()does not type-check arguments; it assumes that the arguments you’ve given match the format specifiers you have given in the string argument.In other words, you are invoking undefined behavior. The type code
%dexpects anintas an argument, but you are specifying aNODEobject instead. Anything at all might happen here — your program might crash, for example (though this is unlikely). At the very least, you cannot rely on this particular output to be consistent across platforms or compilers.If you turn your compiler warnings up to the maximum, it should warn you about this. Modern compilers will type-check arguments to
printf()for you (assuming that you provide a string literal as the first argument, and not a variable or expression), but this is purely a compile-time check and it will only generate warnings, not errors.