出现分段错误的原因是什么? 在下面的代码中,我试图创建一种使用链表的简单方法。 基本上在我的程序中,您只需使用linkedList结构类型创建一个列表。 但是,程序中有一行会导致分段错误。为什么会这样? 任何帮助将非常感激。谢谢:))
#include <stdio.h>
#include <stdlib.h>
struct node{
int num;
struct node *next;
};
//Make the struct
typedef struct {
struct node * first;
struct node * current;
}linkedList;
void addNode(linkedList list, int a);
void addFirstNode(linkedList list, int b);
//Function prototypes
int main() {
linkedList list;
addFirstNode(list, 1);
addNode(list, 2);
addNode(list, 3);
addNode(list, 4);
addNode(list, 5);
addNode(list, 6);
addNode(list, 7);
}
void addFirstNode(linkedList list, int input) {
list.first = (struct node *)malloc(sizeof(struct node)); //Make first node
list.first -> num = input; //Fill first node
list.current = list.first;
}
void addNode(linkedList list, int input) {
struct node * newNode = (struct node *)malloc(sizeof(struct node));
newNode -> num = input;
list.current -> next = newNode; //Segmentation fault! (core dumped)
list.current = newNode;
}
答案 0 :(得分:0)
正如评论中所指出的,您的代码中有许多内容需要纠正:
这是代码的正确版本:
New-Object -TypeName PSCustomObject -Property ([ordered] @{
'name' = $null
'baseAmi' = $null
'patchDate' = $null
'baseName' = $null
'owner' = $null
'instanceID' = $null
'imageID' = $null
'env' = $null
'instanceState' = $null
}) | Format-List