- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
作为作业的一部分,我应该在 c 中实现一个单链表。我之前已经用几种不同的语言做过很多次了,但是在经历了几个小时的痛苦之后,我在使用 strcmp 时遇到了一个问题。这是我正在使用的结构:
typedef struct node {
char *name;
float score;
struct node *next;
} node;
这个问题是特定于插入函数的,它应该类似于插入排序,因为我需要将列表中的节点按字母顺序排序。(我的教授指定插入函数进行排序,尽管没有称其为插入排序)。
void insert(node **start, char *name, float score) { // to insert a record into the linked list sorted by name in dictionary order.
//create new node
node *n_node = new_node(name, score);
node *current;
current = *start;
if (current != NULL) { //-----------if list is not empty
node *prev = NULL;
if (current->next != NULL) { //--if list has more than 1 element
while (current != NULL && strcmp(name, current->name) > 0) { //cycle through list to sorted insertion point
// ^^^^^^^Problem Here^^^^^^^^
//while name is greater than current name, means lower on alphabet (z>a)
prev = current;
current = current->next;
}
if (current != NULL) { //-----not at end of list
//once current is not < new node, connect between prev and current
prev->next = n_node;
n_node->next = current;
} else { // ------------------at end of list
prev->next = n_node;
}
} else { //-----------------------list has only one element
current->next = n_node;
}
} else { //--------------------------List is empty - assign new node as first element
*start = n_node;
}
}
问题是我的程序崩溃并烧毁,没有任何错误或警告(我正在使用 Eclipse 和 CDT)。该程序运行良好,当while (current != NULL && strcmp(name, current->name) > 0)
修改为while (current != NULL/*&& strcmp(name, current->name) > 0*/)
.
对我来说,显然 name
或 current->name
导致 strcmp
的操作出现问题,但我可以'似乎无法解决这个问题。
编辑:我要补充的是,该函数是从另一个函数调用的,该函数从包含名称和标记对的文件中检索并标记字符串,但我的测试并未表明它通过调用传递了错误的字符串或字符。
有关一些额外的详细信息,这是我的 new_node 函数:
node *new_node(char *name, float score) {
node *new = (struct node*) malloc(sizeof(struct node));
new->name = malloc(strlen(name) + 1);
strcpy(new->name, name);
new->score = score;
new->next = NULL;
return new;
}
(我意识到使用 new
作为节点名称并不明智,我会更改它)以及调用插入的函数:
int data_import(node **startp, char *infilename) { // to import data from the file and insert .
int max_line = 100;
char line[max_line];
char delimiters[] = ",";
char name[500] = "";
char *namep;
namep = &name[0];
float score = 0.0f;
int i = 0;
FILE *fi;
char *token;
// open file to read
fi = fopen(infilename, "r");
if (fi == NULL) { // Cannot open the file.
perror("error");
return 0;
}
// read each line, increase counter, retrieve data
while (fgets(line, max_line, fi) != NULL) {
//fputs(line, stdout); //console output confirmation
token = strtok(line, delimiters);
strcpy(namep, token);
token = strtok(NULL, delimiters); //increment token to mark variable
score = atof(token);
insert(startp, namep, score);
i++;
}
//close file
fclose(fi);
return i;
}
最佳答案
如果您将名为 apple 的元素作为第一个元素,并且尝试添加名为 about 的元素,会发生什么情况?
您将立即被抛出下面的 while 循环,并且您的上一个将被取消分配:
while (current != NULL && strcmp(name, current->name) > 0) { //cycle through list to sorted insertion point
// ^^^^^^^Problem Here^^^^^^^^
//while name is greater than current name, means lower on alphabet (z>a)
prev = current;
current = current->next;
}
这个特定部分对我来说看起来很可疑:
之后您将进入以下例程:
if (current != NULL) { //-----not at end of list
//once current is not < new node, connect between prev and current
prev->next = n_node;
n_node->next = current;
}
因为您的 *prev 未分配,并且您尝试访问它(prev->next = n_node;)。您将在这里崩溃。
关于c - 链表插入中的 strcmp 导致程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48699110/
我想知道如果在同一个函数中多次使用 strcmp() 会返回不同的值。下面是程序。第一种情况我知道它为什么打印 -6。但是在第二种情况下,为什么会打印-1呢? #include #include in
所以我们在 PHP 中得到了这个函数 strcmp(string $1,string $2) // returns -1,0, or 1; 然而,我们没有 intcmp();所以我创建了一个: fun
我正在比较用户输入的用户名和密码。正在比较的字符串正在从文件中读入。无论出于何种原因,它都会适当地比较用户名,而不是密码。。控制台输出:
我正在比较用户输入的用户名和密码。正在比较的字符串正在从文件中读入。无论出于何种原因,它都会适当地比较用户名,而不是密码。。控制台输出:
虽然 word 和 s2 相同,但是 if(strcmp) 语句没有被执行。错在哪里??我检查了从文件中读取的代码并尝试了“printf”以查看 word 和 s2 是否相同,发现它们在大约 10 次
是不是只有静态存储类型的变量不会被函数的返回值初始化,因为它们不被认为是常量?根据这个论点,以下声明在 C 中是否有效。它编译时没有错误或警告, 并且输出符合预期。 #include #includ
我必须生成一个反向链表,这些是我的先决条件 定义结构: struct node { int data; struct node * link; } 包含函数 append --- 在链
我是 C 编程的新手。我正在尝试比较两个字符串。我收到错误:此行有多个标记。传递 'strcmp' 的参数 1 使指针来自整数而不进行强制转换。传递 'strcmp' 的参数 2 使指针来自整数而不进
请注意,我已经检查了与此标题相关的问题,但从我的角度来看,它们与此问题无关。 最初我以为 program1 和 program2 会给我相同的结果。 //Program 1 char *a = "ab
你好,我正在使用 fgets 对我的代码进行循环,我希望当用户引入单词 "bye" 时程序结束,所以我有一个像这样的时间: char buf[1000] do{ fgets(buf, 1000
我想了解为什么我的代码会崩溃。我有一个结构数组,如下所示: typedef struct contact { char cFirstName[10]; char cLastName[1
代码: #include #include #include int main() { int n = strcmp("hello","help"); printf("%d\
这个问题在这里已经有了答案: Crash or "segmentation fault" when data is copied/scanned/read to an uninitialized po
该程序执行以下操作: 扫描文本字符串char input[15]; 将其与字符密码[ ] = "1sure";进行比较 如果字符串不匹配则循环。 如果字符串匹配则终止。 当字符串不匹配时程序会循环。但
我正在读一本 C 语言的书,并且看过这两个 strcmp 算法。 我已经了解了 for 循环的使用原理。 但这两个for循环对我来说是新的。我不明白这些部分 for (i = 0; s[i] == t
用 gcc C99 编译 我正在尝试使用字符串比较来比较 2 个字符串。但是,我似乎在 strcmp 行上得到了堆栈转储。 **属性将包含这些,所以我正在寻找框架类型。 [name] [time] [
我正在努力填补计算机科学方面的自学空白,并参加 Edx 上的 CS50 类(class)。我对 C 完全陌生。在其中一个问题集中,我必须比较使用 crypt 函数加密的字符串。 在下面的示例中,我无法
谁能解释一下 strcmp 在 C 编程中使用什么算法来比较两个字符串? 我不明白它的返回值,它使用任何算法,如“Levenstien 算法”来找出两个字符串之间的距离... 最佳答案 标准 C 库
我在结构数组 books 上有一个delete 函数。我向它传递了一组记录、书的作者 和书名 以及列表大小。 现在这里 假设 list[0].author 和 list[5].author 和 aut
这个问题已经有答案了: using fgets and strcmp in C [duplicate] (3 个回答) 已关闭 4 年前。 作为初学者,我一直在尝试该库的一些功能 string.h并且
我是一名优秀的程序员,十分优秀!