2

I am looking over some OpenGL ES code to multiplay matrices, but I'm not sure about how this if statement works:

for (int i = 0; i <_uniformArraySize; i++) {
    **if (!strcmp(_uniformArray[i].Name, "ModelViewProjectionMatrix")) {**

        GLKMatrix4 modelViewProjectionMatrix = GLKMatrix4Multiply(_projectionMatrix, _modelViewMatrix);
glUniformMatrix4fv(_uniformArray[i].Location, 1, GL_FALSE, modelViewProjectionMatrix.m);
    }
}

Does !strcmp mean that the strings are equal or not equal? I looked at the strcmp documentation and it returns numbers. So how does this exclamation point in an if statement affect a number (being the return value of strcmp)?

Thanks

foobar5512
  • 2,470
  • 5
  • 36
  • 52

3 Answers3

3

Since Objective C, like C, allows integers in conditionals, using !expr is a common shorthand for expr== 0.

Your statement is equivalent to

if (strcmp(_uniformArray[i].Name, "ModelViewProjectionMatrix") == 0) {
    ...
}

Since strcmp returns zero when strings are equal to each other, the condition checks if the content of two C strings is the same.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

strcmp() function returns the total number of differences found between each string, so a zero means there are no differences. It is kind of un-intuitive when you think of it in terms of strcmp true/false.

The exclamation mark is the 'NOT' operator. This means that it will test whether the value in front of it is NOT true, so the result is essentially a reversal of the original boolean value.

In this case, if !strcmp() means if the result of strcmp is NOT > 0 then the result is true.

Lochemage
  • 3,974
  • 11
  • 11
0

strcmp will return zero when strings are equal. The exclamation point is the negation operator so the program will enter the if statement when strings are equal.

dbv
  • 427
  • 4
  • 13