使用strcmp段错误() [英] Segmentation fault with strcmp()

查看:490
本文介绍了使用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上面的说法命令行参数运行它。


您可以通过两种方式实现这一点:


  1. 主要的语法是:

      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]);
     }


  2. 第二种方法是使用第二个参数:的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 to strcmp() 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 == as if(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:

  1. 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]);
     }
    

  2. Second technique is using second argument: argv[] is NULL terminated array of string strings so argv[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 and argv[2] == lastname and argv[3] == NULL, Note this time argc == 3 (argv[argc] means argv[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 use argv[0] instead of hard code name of your executable while writing code, so that if you recompile and give new name to your executable then argv[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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆