gpt4 book ai didi

c - 结构通过引用问题

转载 作者:太空宇宙 更新时间:2023-11-04 06:22:41 24 4
gpt4 key购买 nike

<分区>

我在用 C 语言初始化节点值以传递指针时遇到问题,

我已经写了类似 follow 的东西,

#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};

void add(struct node *head, int val){
struct node *n1 = NULL;
n1 = (struct node *)malloc(sizeof(struct node ));
n1 -> data = val;
n1 -> next = NULL;

if(head == NULL){
head = n1;
printf("Head2 is initialized");
return;
}
}


int main(){
struct node *ptr = NULL;
struct node *temp;
add(ptr, 11);
printf("\nData = %d", ptr->data);
return 0;
}

你能告诉我这段代码有什么问题吗,

当我执行

printf("\nData = %d", ptr->data);

系统显示 Windows 已停止工作

谢谢

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com