无法从函数返回字符串

时间:2020-01-02 05:21:25

标签: c string linked-list

list.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调用此函数时,我无法检索 返回的字符串(当前->值)并进行细分 故障,但可以使用相同的功能进行打印。

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

遇到细分错误。

1 个答案:

答案 0 :(得分:1)

  1. list_add函数期望(指向)类型节点的数组(非预期):void list_add(node **handle, ...,但是您要从主节点list_add(&handle,"a","1");

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

  3. 不要将堆栈内存(字符数组)返回给调用方(主):return "";可以,因为“”仅是一个字符('\ 0'),但是最好定义堆占位符表示“空”返回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;
    }

}