【发布时间】:2020-12-07 12:32:28
【问题描述】:
当我尝试为 *temp 赋值时,它没有赋值(当我编译时,它不显示 printf 并且 printf 看不到任何赋值)。为什么 ?如何处理更多关于指针的信息(查看它们在我的 IDE 中使用外部应用程序引用的位置...?)
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#define INT_SIZE sizeof(int) * 8
typedef struct Node Node;
struct Node
{
int value;
Node *next;
};
typedef struct LinkedList
{
Node *head;
}LinkedList;
void Insert(LinkedList **lst, int data)
{
Node *temp = malloc(sizeof(Node));
//Check's if is the first Node.
if ((*lst)->head->next== NULL)
{
(*lst)->head->next = temp;
temp->value = data;
printf("Ok");
temp->next = NULL;
}
}
还有我的主要功能:
int main()
{
LinkedList *list = malloc(sizeof(LinkedList)); //Create new linkedlist
list->head->next = NULL; //Define the head object
Insert(&list, 20);
return 0;
}
【问题讨论】:
-
你怎么称呼
Insert? -
您打算如何在
(*lst)->head位置得到一个有效的、已分配的、已初始化的项目?如果你不这样做,那是你的问题。 -
在你的主
list->head没有初始化,你不能访问list->head->next -
@Jose 我已经编辑了帖子并添加了主要功能。
-
好的,编辑后:
list->head没有指向任何地方,所以您无法访问lists->head->next...
标签: c struct linked-list singly-linked-list function-definition