我使用的strcmp在以下方式段错误使用strcmp
- 传递的char []数组名称
传递指针字符串文字 但是,第二个结果中赛格故障。即使我已经证实,指针指向正确的字符串文字,我很困惑,为什么我收到赛格故障.. 下面是代码: -
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char const *args[]) { char firstName[strlen(*++args)]; strcpy(firstName, *args); char lastName[strlen(*++args)]; strcpy(lastName, *args); printf("%s\t%s\n", firstName, lastName); printf("%d\n", strcmp(firstName, lastName));// this works printf("%d\n", strcmp(*(--args),*(++args)));//this gives me a seg fault return EXIT_SUCCESS; }
我将其保存为海峡。 c和当我编译它,首先我得到以下警告:
[[email protected] examples]$ gcc -Wall str.c -o str
str.c: In function ‘main’:
str.c:15: warning: operation on ‘args’ may be undefined
终于运行它,给人以赛格故障如下图所示
[[email protected] examples]$ ./str Jimmy Neutron
Jimmy Neutron
-4
Segmentation fault (core dumped)
投票“太局部化”在这根本没有意义。这可以说是http://stackoverflow.com/questions/7877258/why-does-this-ii-3-generate-a-warning-may-be-undefined的重复,因为真正的问题是“这个警告是什么意思? “但过于本土化就是不正当的:无助和缺少重点。 – Flexo 2012-03-16 10:12:32