小编Ahm*_*zan的帖子

将字符串作为指针或文字传递时,strcmp()返回值不一致

strcmp当我注意到这一点时,我正在玩,这里是代码:

#include <string.h>
#include <stdio.h>

int main(){

    //passing strings directly
    printf("%d\n", strcmp("ahmad", "fatema"));

    //passing strings as pointers 
    char *a= "ahmad";
    char *b= "fatema";
    printf("%d\n",strcmp(a,b));

    return 0;

}
Run Code Online (Sandbox Code Playgroud)

输出是:

-1
-5
Run Code Online (Sandbox Code Playgroud)

不应该strcmp一样吗?为什么,我给我传递一个字符串作为不同的值"ahmad"或作为char* a = "ahmad".将值传递给函数时,它们是否在其堆栈中分配?

c c++ linux strcmp

32
推荐指数
2
解决办法
1852
查看次数

标签 统计

c ×1

c++ ×1

linux ×1

strcmp ×1