小编Mr.*_*tus的帖子

更改链表中字符串的值

我是 C 的初学者,我希望有人可以帮助我。因此,我一直在尝试将字符串名称的值更改为另一个值,但是当我使用scanf. 例如,如果我像这样用函数手动插入push(&head, "Carlos")值,名称的值就会改变。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Node{
    char *name;
    struct Node *next;
};


void printList(struct Node *n){
    while (n != NULL){
        printf(" name: %s \n ", n->name);
        printf("....................................\n");
        n = n->next;
    }
}

void push(struct Node **head_ref,  char *name){
    struct Node *new_node = (struct Node *)malloc(sizeof(struct Node));
    new_node->name = name;

    new_node->next = (*head_ref);

    (*head_ref) = new_node;
}

int main(){
    struct Node *head = NULL;

    char name[20];

    printf("Insert a name");
    scanf("%s", …
Run Code Online (Sandbox Code Playgroud)

c string linked-list

2
推荐指数
1
解决办法
57
查看次数

标签 统计

c ×1

linked-list ×1

string ×1