strcmp behaviour

后端 未结 3 2018
死守一世寂寞
死守一世寂寞 2021-02-05 14:37

When I run the following code:

#include 

int main(int argc, char *argv[])
{
    int p = 0;

    p = strcmp(NULL,\"foo\");

    return 0;
}
         


        
3条回答
  •  隐瞒了意图╮
    2021-02-05 15:03

    You need to:

    • Include the proper headers, or declare functions manually. For strcmp(), you need .
    • Not pass an invalid pointer such as NULL to strcmp(), since it doesn't protect against it and will dereference the pointer, thus causing undefined behavior in your program.

提交回复
热议问题