所以我试图制作一个链表列表,起初我有以下代码:

typedef struct node{
    int data;
    struct node *next;
} node;

struct ko {
    struct node *first;
    struct node *last;
} ;

struct ko array[6];


使用此代码,程序的其余部分可以正常运行,但是,我希望用户能够在程序启动时定义数组大小,经过一些研究,我认为应该是这样的:

.h文件

typedef struct node{
    int data;
    struct node *next;
} node;



struct ko {
    struct node *first;
    struct node *last;
} ;

struct ko *array;


main.c文件:

int size;
printf("array size: ");
scanf("%d", &size);
array = malloc(sizeof(struct ko) * size);


使用此代码,程序可以很好地编译,但是它在运行时会卡住,不会崩溃,它什么都不会做...所以我想知道问题出在此代码中还是其他地方?程序..
谢谢

最佳答案

好吧,这里没有什么是立即出错的...只需确保将数组内容初始化为零(实际上100%可移植的方式是使用for循环,但使用calloc()可以在所有常用平台上使用)。

关于c - 动态分配链表列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16710170/

10-12 12:29
查看更多