使用strcmp段错误() [英] Segmentation fault with strcmp()
问题描述
如果(STRCMP(的argv [2],NULL)== 0)
我路过3命令行参数,但我也想只有2上面的说法命令行参数运行它。但是,正在显示分段错误。
我也试过用
如果(ARGC 3;)
但它也没有工作......同分段错误...
为什么要分割的错吗?
块引用>由于code的
如果(STRCMP(的argv [2],NULL)== 0)
,你传递NULL作为字符串的指针的strcmp()
功能;试图在NULL来尊重比较字符codeS(例如ACSII code)在运行时,这个事业不确定的行为。您应该使用
比较空字符串的指针==
为如果(的argv [2] == NULL)
我路过3命令行参数,但我也想只有2上面的说法命令行参数运行它。
块引用>您可以通过两种方式实现这一点:
主要的语法是:
INT主(INT ARGC,CHAR *的argv [])
第一个参数
ARGC
是专柜的说法是传递给你的进程,包括进程名的参数总数。所以,当你没有传递额外的参数则
的argc == 1
例如./ EXE
假设,如果你传递三个参数如下:
./ exe文件名字姓氏
然后
的argc == 3
,
它看起来像你逝去的两个参数,但包括你实际上是通过三个参数来处理可执行文件的名称。所以,你可以使用
ARGC
的价值在一个循环遍历打印传递的参数(其他然后可执行文件)的printf(进程名称是:%S的argv [0]);
对于(i = 1; I< ARGC,我++){
的printf(的argv [%D]。%S \\ N的argv [I]);
}
第二种方法是使用第二个参数:
的argv [
]是NULL结尾的字符串的字符串数组,以便ARGV [ARGC]
总是等于NULL。您可以使用循环这些信息进行迭代和传递的参数的过程。要了解此假设您正在执行的函数为:
./ exe文件名字姓氏
然后
的argv [0] == ./exe
,的argv [1] ==姓
和的argv [2] ==姓
和的argv [3] == NULL
,注意这个时间ARGC == 3
(ARGV [ARGC]
办法的argv [3]
== NULL) 。例如打印所有参数,你可以写你code,如:
INT I = 1;
的printf(进程名称是:%S的argv [0]);
而(的argv [I]){//终止时的argv [I] == NULL
的printf(的argv [%D]。%S \\ N的argv [I]);
我++;
}
你注意到
的argv [0]
永远是你的可执行文件的名字!这意味着当你需要打印您的可执行文件名称使用的argv [0]
而不是可执行文件的硬code的名字,而写code,因此,如果您重新编译和可执行文件赋予新的名称,然后的argv [0]
始终打印正确的名称。你应该写code如下:INT主(INT ARGC,CHAR *的argv []){
:
://一些其他的code
如果(ARGC< min_number_of_arguments){
fprintf中(标准错误,错误:参数错号通过\\ n);
fprintf中(标准错误,用法:%s的[首页] [秒] \\ n,argv的[0]);
出口(EXIT_FAILURE);
}
:
://一些其他的code
返回EXIT_SUCCESS;
}if(strcmp(argv[2], NULL) == 0)
I'm passing 3 command line arguments but I also want to run it with only 2 command line arguments with the above statement. But a segmentation fault error is being displayed.
I also tried with
if(argc < 3)
but it also didn't work...same segmentation fault...
解决方案Why segmentation fault?
Because of code
if(strcmp(argv[2], NULL) == 0)
, you are passing NULL as string pointer tostrcmp()
function; that try to deference at NULL to compare chars codes (e.g. acsii code) this cause undefined behavior at run time.You should compare a string pointer with NULL using
==
asif(argv[2] == NULL)
I'm passing 3 command line arguments but I also want to run it with only 2 command line arguments with the above statement.
You can implement this in two ways:
The main syntax is:
int main(int argc, char* argv[])
The first argument
argc
is argument counter that is total number of arguments passed to your process including process name.So when you pass no extra argument then
argc == 1
e.g../exe
Suppose if you pass three arguments as follows:
./exe firstname lastname
Then
argc == 3
, it looks like you are passing two arguments but including executable name you are actually passing three arguments to process.So you can use of
argc
value to iterate in a loop to print arguments passed (other then executable)printf("Process name is: %s", argv[0]); for(i = 1; i < argc; i++ ){ printf("argv[%d] %s\n", argv[i]); }
Second technique is using second argument:
argv[]
is NULL terminated array of string strings soargv[argc]
is always equals to NULL. You can use this information in loop to iterate and process of arguments passed.To understand this suppose you are executing function as:
./exe firstname lastname
then
argv[0] == ./exe
,argv[1] == firstname
andargv[2] == lastname
andargv[3] == NULL
, Note this timeargc == 3
(argv[argc]
meansargv[3]
== NULL).For example to print all arguments, you can write you code like:
int i = 1; printf("Process name is: %s", argv[0]); while(argv[i]){// terminates when argv[i] == NULL printf("argv[%d] %s\n", argv[i]); i++; }
Do you notice
argv[0]
is always your executable name! this means whenever you need to print your executable name useargv[0]
instead of hard code name of your executable while writing code, so that if you recompile and give new name to your executable thenargv[0]
always prints correct name. You should write code as follows:int main(int argc, char* argv[]){ : :// some other code if(argc < min_number_of_arguments){ fprintf(stderr, "Error: wrong number of arguments passed!\n"); fprintf(stderr, "Usage: %s [first] [second] \n", argv[0]); exit(EXIT_FAILURE); } : :// some other code return EXIT_SUCCESS; }
这篇关于使用strcmp段错误()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!