簡體   English   中英

strcmp 導致 C 中的段錯誤

[英]strcmp causing seg fault in C

使用strcmp時,我似乎遇到了段錯誤:奇怪的是,該值似乎是存儲的正確值,但仍會導致段錯誤。 有誰知道為什么這是我的代碼?

比如說我有 'xx' 作為我想在 for 循環之后的 2 行中取消別名的值,它們都 = 'xx' 但是if (alias_array[i].alias.= NULL && strcmp(alias_array[i],alias, arguments[1]) == 0) {

typedef struct alias {
    char alias[50];
    char command[50];
} alias;

alias alias_array[MAX_ALIAS_LEN];

void unalias_command(char *arguments[50]) {
    char alias_name[50];
    strcpy(alias_name, arguments[1]);
    printf("args[1] %s\n", *arguments);
    printf("Len of args = %ld", strlen(arguments[1]));
    //if(strcmp(alias_name, NULL) != 0) {      
    if (arguments != NULL) {
        int found = 0;
        for (int i = 1; i < MAX_ALIAS_LEN; i++) {
            printf("current name = %s\n", alias_array[1].alias);
            printf("current argument = %s\n", arguments[1]);
            if (alias_array[i].alias != NULL && strcmp(alias_array[i].alias, arguments[1]) == 0) {
                // if alias is found and matches then we can use the free() function
                found = 1;
                strcpy(alias_array[i].alias, NULL);
                strcpy(alias_array[i].command, NULL);
                break;
            }
        }
        if (found == 1) {
            printf("Alias, %s has been removed as an alias\n", arguments);
        } else {
            printf("No alias with name %s\n", arguments[1] );
        }
    } else {
        printf("Error: command 'unalias' requires 1 argument"); }
}

... 

char *args[50] = {""}
unalias_command(args)

如果您傳遞包含單個空字符串和 50 個 null 指針的字符串數組:

    char *args[50] = {""}
    unalias_command(args)

您在這里遇到分段錯誤:

   strcpy(alias_name, arguments[1]);

和這里:

   printf("Len of args = %ld", strlen(arguments[1]));

進一步的問題:

  • alias結構的成員不是指針,因此比較alias_array[i].alias != NULL沒有意義
  • strcpy(alias_array[i].alias, NULL); 具有未定義的行為。 你應該寫strcpy(alias_array[i].alias, ""); 或者只是*alias_array[i] = '\0';

您應該傳遞 arguments 的計數和參數數組,就像main()接收的那樣,並且在繼續處理命令之前必須檢查是否正確使用:

#include <stdio.h>
#include <string.h>

typedef struct alias {
    char alias[50];
    char command[50];
} alias;

alias alias_array[MAX_ALIAS_LEN];

int unalias_command(int argc, char *arguments[50]) {
    char *alias_name;
    if (argc < 2) {
        fprintf(stderr, "unalias: missing argument\n");
        return 1;
    }
    // no need to copy the alias, just use a char pointer
    alias_name = arguments[1];
    if (alias_name[0] != '\0') {
        int found = 0;
        for (int i = 0; i < MAX_ALIAS_LEN; i++) {
            if (strcmp(alias_array[i].alias, alias_name) == 0) {
                // if alias is found and matches then we can reset it
                found = 1;
                *alias_array[i].alias = '\0';
                *alias_array[i].command = '\0';
                break;
            }
        }
        if (found) {
            printf("Alias `%s' has been removed as an alias\n", alias_name);
            return 0;
        } else {
            printf("No alias with name `%s'\n", alias_name);
            return 1;
        }
    }
}

請注意,檢查if (alias_array[i].alias != NULL)實際上沒有做任何有意義的事情。 .alias成員被聲明為一個固定char[50]數組,因此,在該表達式中,它將衰減為一個指針(指向該數組的第一個元素)。 那不可能是NULL (在任何有意義的數據集中)。

您需要一些其他方法來測試alias_array[i].alias成員是否表示有效字符串。 可能, if (alias_array[i].alias[0] != '\0')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM