c - 将唯一 char* 添加到链表时出现问题
问题描述
我正在尝试实现一个数据结构,其中我有一个 char* 值列表和一个数组,该数组存储来自 cmd 行的每个唯一 char* 值的出现次数。这有点乱,但我想我已经弄清楚了,直到我尝试测试它(每个测试不必以不同的方式编译,我只是重命名每次运行以使测试名称不同)。
测试 cmd 输入:
test1 a b c d e f g
test2 a a b b c d d e f f g g
test3 a a a a a a a a a a a
test4 a a a a a a a a a a a b c c
test5 a j i q y z n f o p m a
test6 a a b b c d d e f f g
test7 a j k q e s l i h i a
test8 a j i q y z n f o p m
根据这些输入,每个表格的长度应如下所示:
test1 table->length = 7 <- works
test2 table->length = 7 <- works
test3 table->length = 1 <- works
test4 table->length = 3 <- works
test5 table->length = 11 <- works
test6 table->length = 7 <- no work
test7 table->length = 9 <- no work
test8 table->length = 11 <- no work
基于这些结果,我不相信它的长度argc
(因为测试 1、2、3 有效)也不是唯一*char
s 的数量(根据测试 2、3、4,5)。运行下面的代码时,它可以工作并且我到达末尾main(...)
,或者,根据输入,它会在显示后失败:
...
ptr address: 00DF04F4
ptr value: 00DF0510
测试.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node {
char *path;
int index;
struct node *next;
} node;
typedef struct lookup {
struct node *head;
struct node *ptr;
int length;
int array[];
} lookup;
void add(lookup *table, char *path);
void search(lookup *table, char *path, int *i);
void add(lookup *table, char *path) {
int i = 0;
search(table, path, &i);
if(table->ptr == NULL) {
node *new_node = malloc(sizeof(node));
if (table->head == NULL) {
table->head = new_node;
table->head->next = NULL;
table->head->path = path;
table->array[0] = 1;
table->head->index = 0;
table->length = 1;
} else {
new_node->path = path;
new_node->next = table->head;
table->head = new_node;
table->array[i] = 1;
table->head->index = i;
table->length += 1;
}
} else {
table->array[table->ptr->index] += 1;
}
}
void search(lookup *table, char *path, int *i) {
if(table->head == NULL) {
table->ptr = NULL;
return;
}
table->ptr = table->head;
(*i) = 0;
while(table->ptr != NULL) {
printf("ptr address: %p\n", &(table->ptr));
printf("ptr value: %p\n", (table->ptr));
/* FAILURE POINT */
printf("%s\n", table->ptr->path);
printf("comparing ptr string: '%s' with given '%s'\n", table->ptr->path, path);
if(strcmp(table->ptr->path, path) == 0) {
printf("found match!\n");
return;
}
table->ptr = table->ptr->next;
(*i) += 1;
}
printf("could not find '%s' in table\n", path);
}
int main(int argc, char **argv) {
lookup *table = malloc(sizeof(lookup) + argc);
table->head = NULL;
int i = 1;
for(i; i < argc; i++) {
printf("\n\ntry adding: %s\n", argv[i]);
add(table, argv[i]);
}
printf("\n\n############\nfinished adding\n");
printf("table length: %d\n", table->length);
return 0;
}
我知道指针不为空(因为我之前打印了它的地址和值)但我不确定为什么它会在看似正常的输入处停止执行。预期的输出(基于当前代码)应该显示每个char*
尝试添加的内容、search(...)
搜索表时指针的地址和值、当前*char
ptr 指向、*char
正在比较的两个 s、是否匹配找到,或者如果search(...)
找不到*char
。
解决方案
推荐阅读
- linux - 如何在 Linux 中获取终端的当前当前光标位置?
- php - 如何使用php和mysql在fpdf中动态创建多个身份证
- python - PyQt5 显示全屏对话框
- java - 为什么我收到 java.io.IOException: stream is closed 异常?
- javascript - Bootstrap 4 下拉配置文件
- python - 在用户输入列表中满足条件时停止 while 循环
- r - 旋转ggplot2中的图例键
- python - 将netifaces安装到本地目录
- r - How can I change axis' scale(or intervals)?
- ssl - HTTP 客户端错误:asn1:结构错误:base 128 整数太大