当使用strcmp()时,我得到一个段错误。我曾尝试使用gdb进行调试,当我运行它(没有参数)时,我得到一个段错误。当我回溯时,它表示段错误在第20行,但我无法弄清楚它为什么会出现段错误。 strcmp()语句在第20行。我将在我的文章中包含第0-21行,但如果需要可以添加更多。C - 段错误使用strcmp()
该段错误的语句,当我使用run
命令如下:
Program received signal SIGSEGV, Segmentation fault.
__strcmp_sse2() at ../sysdeps/x86_64/multiarch/../stcmp.S:213
213 ../sysdeps/x86_64/multiarch/../strcmp.S: No such file or directory.
于是我做了backtrace
,并得到这个:
#0 __strcmp_sse2() at ../sysdeps/x86_64/multiarch/../strcmp.S:213
#1 0x0000000000400aa6 in main (c=1, v=0x7fffffffead8) at myls.c:20
下面是从0-21行我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <time.h>
#include <fcntl.h>
#include <pwd.h>
#include <string.h>
#include <grp.h>
int main(int c, char *v[])
{
DIR *directory = NULL;
strut direct *dir_pointer = NULL;
int i = 0;
char cwd[1024];
if(strcmp(v[1], "-i") == 0){ //line 20, where the segfault happens
...
任何提示/帮助赞赏。
“当我运行它(没有参数)” - 呃???你有*更好*提供至少一个参数,或者按照标准'v [1]'是NULL; 'strcmp'与'NULL'不能很好地兼容,因为它的参数* * *。 – WhozCraig
@WhozCraig关于此的一些背景:我正在编写自己的ls程序,模仿unix'ls'命令。当我用'。/ myls'运行它时,那c = 1是正确的?当我用'./myls -l'运行c = 2时?和'-l' = v [1]? –
我强烈建议您仔细阅读以下Kons提供的[链接问答](http://stackoverflow.com/questions/3024197/what-does-int-argc-char-argv-mean)。它回答了这个问题,还有很多*更多。 – WhozCraig