strcmp() 的分段错误 [英] Segmentation fault with strcmp()

查看:35
本文介绍了strcmp() 的分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if(strcmp(argv[2], NULL) == 0)

我传递了 3 个命令行参数,但我也想使用上述语句仅使用 2 个命令行参数来运行它.但是正在显示分段错误错误.

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.

我也试过

if(argc < 3)

但它也不起作用...同样的分段错误...

but it also didn't work...same segmentation fault...

推荐答案

为什么出现分段错误?

Why segmentation fault?

由于代码if(strcmp(argv[2], NULL) == 0),您将NULL作为字符串指针传递给strcmp()函数;尝试在 NULL 处尊重以比较字符代码(例如 acsii 代码),这会导致运行时出现未定义的行为.

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.

您应该使用 == 作为 if(argv[2] == NULL)

我传递了 3 个命令行参数,但我也想通过上述语句仅使用 2 个命令行参数运行它.

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. 主要语法是:

  1. The main syntax is:

int main(int argc, char* argv[])

第一个参数 argc 是参数计数器,它是传递给进程的参数总数,包括进程名称.

The first argument argc is argument counter that is total number of arguments passed to your process including process name.

所以当你不传递额外的参数时 argc == 1 例如./exe

So when you pass no extra argument then argc == 1 e.g. ./exe

假设您按如下方式传递三个参数:

Suppose if you pass three arguments as follows:

./exe firstname lastname    

然后 argc == 3,看起来您正在传递两个参数,但包括可执行文件名称,您实际上是在传递三个参数以进行处理.

Then argc == 3, it looks like you are passing two arguments but including executable name you are actually passing three arguments to process.

因此您可以使用 argc 值在循环中迭代以打印传递的参数(其他可执行)

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

  • 第二种技术是使用第二个参数:argv[] 是字符串字符串的 NULL 终止数组,所以 argv[argc] 总是等于 NULL.您可以在循环中使用此信息来迭代和处理传递的参数.

  • 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] == firstnameargv[2] == lastnameargv[3] == NULL,注意这次argc == 3(argv[argc]的意思是argv[3] == NULL).

    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++;
      }
    

  • 您是否注意到 argv[0] 始终是您的可执行文件名称!这意味着每当您需要打印可执行文件名称时,请在编写代码时使用 argv[0] 而不是可执行文件的硬代码名称,这样如果您重新编译并为可执行文件指定新名称,则 argv[0] 总是打印正确的名称.您应该编写如下代码:

    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天全站免登陆