GCC if statement where variable is not compared to anything after strcmp.
How does the GCC compiler interpret this:
Code:
returnval = strcmp(buffer, BUFLEN) |
Quote:
First, strcmp takes two const char pointers to NULL terminated strings as arguments, so if your variable names indicate their types, BUFLEN and BUFLEN2 are not likely to be valid strings for comparison to buffer. We will ignore absence of statement ending ;'s and treat this as pseudo code. But let's assume that buffer, BUFLEN and BUFLEN2 are all valid pointers to null terminated strings, then I would read the result as follows: If buffer == BUFLEN, returnval == 0, then the second strcmp() will not be executed. If buffer != BUFLEN, returnval != 0, then the second strcmp() will be executed. |
Quote:
Or do you mean how this is translated? Use the gcc -S option. |
Basically an if statement expression i.e. if (expression) evaluates as either true of false with true being a non zero value. Therefore if returnval (in this case it is a valid expression) is not zero the statements within the if block are executed.
|
Quote:
Quote:
Quote:
|
Quote:
|
Actually, it's a bug. (At least, "as written.")
The first two arguments to the strcmp() function must be char *s. In the examples shown, neither second-argument is likely to be such a pointer. If we "glide over the probable typos" here, the intent of this logic is to compare two strings using one of two possible lengths. If the first attempt results in a value that is "true-thy" (i.e. the result is "not-equal"), a second compare is done using a different maximum length. |
All times are GMT -5. The time now is 10:09 PM. |