Search code examples
objective-cstrcmp

Objective-C: Exclamation Point strcmp in "if" statement


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


Solution

  • 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.