【问题标题】:Segmentation Fault: 11 C sometimes分段错误:有时 11 C
【发布时间】:2018-03-27 06:56:48
【问题描述】:

我试图在我的 mac 上运行一个 C 程序,要求用户输入一组名称。然后程序对所有名称进行排序和大写,并将它们打印为大写和排序。然后它允许用户搜索名称。但是,大多数时候(但不是每次)我都尝试运行它返回分段错误的代码:11 错误。我的猜测是这个问题与 fgets 或我的数组有关,但我真的不知道。

这是我的代码:

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

#define SIZE 50
#define LENGTH 50
#define TRUE 1
#define FALSE 0

void printList(char names[SIZE][LENGTH], int length);
void toUpperCase(char names[SIZE][LENGTH], int length);
void sort(char names[SIZE][LENGTH], int length);
void startSearch(char names[SIZE][LENGTH], int length);
int binSearch(char names[SIZE][LENGTH], int l, int r, char x[LENGTH]);


int main(void){
    char names[SIZE][LENGTH]; //stores the list of names
    printf("Enter student names (q to stop)...\n");

    int i = 0;
    do {
        printf("Student name #%d: ", i);
        fgets(names[i], LENGTH, stdin);     //fill the list of names
        int len = strlen(names[i])-1;       //fgets includes \n character
        if(names[i][len] == '\n')           //if the last character is \n
            names[i][len] = '\0';           //change it to \0
        if(strcmp(names[i], "") == 0)
            printf("Invalid input: Type a name\n"); 
        else
            i++;
    }
    while(strcmp(names[i-1],"q")!=0 && i<SIZE); //Stop collecting names after input "q" 
                                                //or if the names array is full

    int length = i-1; //# of names in the names array

    sort(names, length);
    toUpperCase(names, length);
    printList(names, length);
    startSearch(names, length);


    printf("Done!\n");
    return 0;
}

//Converts all the names in the names array to upper case
void toUpperCase(char names[SIZE][LENGTH], int length){
    for(int i = 0; i < length; i++){
        for(int j = 0; names[i][j]!='\n'; j++){
            if(islower(names[i][j]))
                names[i][j] = toupper(names[i][j]);
        }
    }
}

//sorts the names in the names array (bubble sort)
void sort(char names[SIZE][LENGTH], int length){
   int i, j;
   char temp[LENGTH];
   for (i = 0; i < length-1; i++)
       for (j = 0; j < length-i-1; j++)
           if (strcmp(names[j],names[j+1])>0){
                   strcpy(temp, names[j]);
                   strcpy(names[j], names[j+1]);
                   strcpy(names[j+1], temp);

           }
}

//prints the names in the names array
void printList(char names[SIZE][LENGTH], int length){
    printf("Student list: [\n");
    for(int i = 0; i < length; i++)
        if(i == length-1)
            printf("\t%s\n", names[i]);
        else
            printf("\t%s,\n", names[i]);
    printf("]\n");
}

//The first method for searching the list
void startSearch(char names[SIZE][LENGTH], int length){
    char search[LENGTH];
    while(strcmp(search, "q")!=0){
        printf("Enter a name to search (q to exit): "); 
        fgets(search, LENGTH, stdin); //gets the name to search
        int len = strlen(search)-1;
        if(search[len] == '\n')
            search[len] = '\0';
        if(strcmp(search, "q") == 0)     //if entered value is q
            break;                       //break out of the loop
        //Since the list is all upper case change the search value to upper case
        for(int j = 0; search[j]!='\n'; j++){
            if(islower(search[j]))
                search[j] = toupper(search[j]);  
        }

        printf("Searching for %s ...\n", search);
        // if binSearch returns true then the item is in the list
        if(binSearch(names, 0, length-1, search) == TRUE) 
            printf("%s is in the list!\n", search); /
        else
            printf("%s is NOT in the list!\n", search);
    }
}

//binary search for the names array
int binSearch(char names[SIZE][LENGTH], int l, int r, char x[LENGTH]){
    while (l <= r)
    {
    int m = l + (r-l)/2;
    if(strcmp(names[m], x) == 0)
        return TRUE;
    if(strcmp(names[m], x) < 0)
        l = m + 1;
    else
        r = m - 1;
    }
    return FALSE;
}

【问题讨论】:

  • 您应该使用 **names 或 '*names[SIZE]' 而不是 names[SIZE][LENGTH] 来存储名称列表。
  • Carolina:请提供正确的minimal reproducible example - 即崩溃的会话的复制粘贴。
  • @AnttiHaapala 因为名称可以有不同的长度,并且您可以轻松地对 pointers 的数组进行排序,特别是当它指向 char* 时,也很难交换两个不同大小的字符串。跨度>
  • @krpra 好点:D 我没有注意到这里有一个排序

标签: c eclipse macos segmentation-fault


【解决方案1】:

我假设您使用 SIZE 和 LENGTH 的固定数组来进行学习。对于与字符串相关的实际工作,您最好听取 kpra 的建议并使用更复杂但更强大的指针(分配它们并在需要时释放它们)。

在你的阅读循环中,你杀死所有的“\n”,用零替换它们。

然而,在您的toUppercase() 代码中,您寻找的是“\n”而不是 0x0。这可能会破坏缓冲区:

//Converts all the names in the names array to upper case
void toUpperCase(char names[SIZE][LENGTH], int length){
    for(int i = 0; i < length; i++){
        for(int j = 0; names[i][j]!='\n'; j++){
            // what happens here if \n is not found and j exceeds SIZE?
            if(islower(names[i][j]))
                names[i][j] = toupper(names[i][j]);
        }
    }
}

您可以将 \n 替换为 0x0,但我认为更安全的循环是:

     for(int j = 0; j < SIZE; j++) {
        if (yourstring[j] == 0) {
            break;
        }

这样你肯定不会超过 SIZE,如果找到字符串的结尾,循环就会结束。请注意,这个 '\n' 比较也在搜索循环中使用。

【讨论】:

  • 已修复,谢谢。我还以更广泛使用的方式重写了比较。
  • 错误确实是在寻找 '\n' 而不是 '\0'。谢谢!
猜你喜欢
  • 2014-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-22
  • 1970-01-01
相关资源
最近更新 更多