先看测试用例:
#include <stdio.h> int main(int argc, char *argv[]) { char *s = "hello"; char *str = "hello"; printf("compare pointers: "); s == str ? printf("y") : printf("n"); #(1) printf("\ncompare arguments: "); argv[1] == argv[2] ? printf("y") : printf("n"); #(2) printf("\nuse strcmp :"); strcmp(&argv[1], &argv[2]) ? printf("y") : printf("n"); #(3) return 0; } 运行结果:
$ ./a.out tmp tmp compare pointers: y compare arguments: n use strcmp :y 确切的来说是想解决一些关于C和内存知识的问题:
在代码中, (1)比较的两个字符串, s和str在内存中是不同的指向char的指针, 但是他们指向的内容实际上是同一处?
(2)中比较的内容结果是不相等, "=="号比较的是所给参数的内存地址吗?
(3)的输出结果为y, 所以strcmp比较的是指针所指向的内容?

