c - 无法从函数返回字符串
问题描述
列表.c
char *get_value ( node *handle, char *name) {
node *current = handle;
while (current) {
if((!strcmp(current->name, name))) {
printf("=> %s %s %s\n", __func__, current->name, current->value);
return current->value;
}
current=current->next;
}
return "";
}
void list_add(node **handle, char *name, char *value)
{
node *current, *new;
new = (node *)malloc(sizeof(node));
if (value == NULL) {
value = "";
}
strcpy(new->name, name);
strcpy(new->value, value);
new->next = NULL;
current = *handle;
if(*handle == NULL) {
*handle = new;
}
else {
while(current->next != NULL) {
current=current->next;
}
current->next = new;
}
}
从 main.c 调用此函数时,我无法检索返回的字符串(当前-> 值)并获得分段错误,但能够在同一函数中打印。
主程序
struct node {
char name[100];
char value[512];
struct node *next;
};
node handle = NULL;
int main () {
....
list_add(&handle,"a","1");
printf("%s\n", get_value (handle, name));
....
}
出现分段错误。
解决方案
list_add 函数需要(指向)节点类型的数组(非预期):
void list_add(node **handle, ...
但是您正在从 main 传递节点类型的堆指针:list_add(&handle,"a","1");
的初始化
handle
不完整:node handle = NULL;
应该是(类似于)node handle = {"","",NULL};
不要将堆栈内存(char 数组)返回给调用者(main):
return "";
可以,因为 "" 只是一个 char ('\0'),但最好为 'empty' 返回 char 数组定义堆占位符。
工作代码:
struct node_ {
char name[100];
char value[512];
struct node_ *next;
} typedef node;
//declarations - linked list of type node, not array node** structure
char *get_value(node *handle, char *name);
void list_add(node *handle, char *name, char *value);
//initialise heap variable
node handle = {"","", NULL };
char _notFound__[1] = "";
int main() {
//passing address of heap variable 'handle'
list_add(&handle, "a", "1");
//passing address of heap variable 'handle'
printf("%s\n", get_value(&handle, "a"));
return 0;
}
char *get_value(node *handle, char *name) {
//asign placeholder
node *current = handle;
//search linked list
while (current) {
if (!strcmp(current->name, name)) {
printf("=> %s %s %s\n", __func__, current->name, current->value);
//return pointer to heap memory
return current->value;
}
current = current->next;
}
//return pointer to heap char-array memory
return _notFound__;
}
void list_add(node *handle, char *name, char *value)
{
node *current, *new;
//heap memory - requires destruction at heap scope
new = (node *)malloc(sizeof(node));
//initialise (copy construction)
(name) ? strcpy(new->name, name) : strcpy(new->name, "");
(value) ? strcpy(new->value, value) : strcpy(new->value, "");
new->next = NULL;
//insert new item
if (handle == NULL) {
handle = new;
}
else
{
//assign placeholder
current = handle;
//scroll to end
while (current->next != NULL)
{
current = current->next;
}
current->next = new;
}
}
推荐阅读
- javascript - 如何在浏览器中安全地存储 JWT?
- python - 检查熊猫 df 中的值是否在第二个 df 的列中的列表内
- ansible - 恢复使用 Copy 创建的备份文件
- java - 尝试安装现有调试 apk 时出现“未安装应用程序”
- python - 旋转数据框?如何整理 325x1 df
- c# - 在.net 5中使用工厂方法进行依赖注入的对象生命周期
- javascript - 如何使用 concat 将条件字符串传递给 ember 组件属性?
- json - 使用 Scala 表示两个 JSON 字段,其中只有一个可以为 null
- python - Google sheet api- Python/Django,接收来自单元格的隐藏链接
- c++ - 不确定智能指针 C++