首页 > 解决方案 > 无法从函数返回字符串

问题描述

列表.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));
....
}

出现分段错误。

标签: cstringlinked-list

解决方案


  1. list_add 函数需要(指向)节点类型的数组(非预期):void list_add(node **handle, ...但是您正在从 main 传递节点类型的堆指针:list_add(&handle,"a","1");

  2. 的初始化handle不完整:node handle = NULL;应该是(类似于)node handle = {"","",NULL};

  3. 不要将堆栈内存(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;
    }

}

推荐阅读