gpt4 book ai didi

c - 段错误 : 11 C sometimes

转载 作者:太空宇宙 更新时间:2023-11-04 08:01:07 25 4
gpt4 key购买 nike

我正在尝试在我的 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;
}

最佳答案

我假设您出于学习目的使用固定的 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' 比较也用在搜索循环中。

关于c - 段错误 : 11 C sometimes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46758706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com