gpt4 book ai didi

c - C 函数中的动态结构数组

转载 作者:行者123 更新时间:2023-11-30 21:28:51 25 4
gpt4 key购买 nike

我正在尝试创建邻接列表来表示文件“input.txt”中的边列表中的图形。我知道指针如何工作的基础知识,但我对由单链表组成的动态结构数组有问题。

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

struct list {
int vertex;
struct list *next;
};

void create_list(int v, struct list* **array);

int main()
{
int i, v = 5;
struct list *ptr, **array = (struct list **)malloc(sizeof(struct list *) * v);
for (i = 0; i < v; i++)
array[i] = NULL;
array[i] = NULL;

create_list(v, &array);

for(i = 0; i < v; i++) {
ptr = array[i];
printf("%d: ", i);
while(ptr != NULL) {
printf(" ->%d", ptr->vertex);
ptr = ptr->next;
}
printf("\n");
}
return 0;
}

void create_list(int v, struct list* **array)
{
int m, n;
struct list *ptr, *tmp;
FILE *luki;
luki = fopen("input.txt", "r");
while(fscanf(luki, "%d %d\n", &m, &n) == 2) {
tmp = (struct lista*)malloc(sizeof(struct list));
tmp->vertex = n;
tmp->next = NULL;
if (*array[m] == NULL) //Here my program crashes when m changes from 0 to 1
*array[m] = tmp;
else {
ptr = array[m];
while(ptr->next != NULL)
ptr = ptr->next;
ptr->next = tmp;
}
}
fclose(luki);
}

你能帮我弄清楚它应该是什么样子吗?

此外,起初我在不使用指向数组的指针的情况下创建了该函数:

void create_list(int v, struct list **array)

create_list(v, array);

调试时效果非常好(我正在使用 CodeBlocks):

0: 4 ->3 ->1
1: 2
2: 3
3:
4:

但是在正常运行程序时我得到了这个:

0:
1:
2:
3:
4:

如果将数组传递给函数create_list是错误的,为什么调试时的输出是正确的?

最佳答案

(1) void create_list(int v, struct list **array); ... create_list(v, 数组);

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

struct list {
int vertex;
struct list *next;
};

void create_list(int v, struct list **array);

int main(void) {
int i, v = 5;
struct list *ptr, **array = (struct list **)malloc(sizeof(struct list *) * v);
for (i = 0; i < v; i++)
array[i] = NULL;

create_list(v, array);

for(i = 0; i < v; i++) {
ptr = array[i];
printf("%d: ", i);
while(ptr != NULL) {
printf("%d", ptr->vertex);
ptr = ptr->next;
if(ptr)
printf(" -> ");
}
printf("\n");
}
return 0;
}

void create_list(int v, struct list **array){
int m, n;
struct list *ptr, *tmp;
FILE *luki;
luki = fopen("input.txt", "r");
while(fscanf(luki, "%d %d\n", &m, &n) == 2) {
tmp = (struct list*)malloc(sizeof(struct list));
tmp->vertex = n;
tmp->next = NULL;
if (array[m] == NULL)
array[m] = tmp;
else {
ptr = array[m];
while(ptr->next != NULL)
ptr = ptr->next;
ptr->next = tmp;
}
}
fclose(luki);
}
<小时/>

(2) void create_list(int v, struct list ***array); ... create_list(v, &array);

void create_list(int v, struct list ***array){
int m, n;
struct list *ptr, *tmp;
FILE *luki;
luki = fopen("input.txt", "r");
while(fscanf(luki, "%d %d\n", &m, &n) == 2) {
tmp = (struct list*)malloc(sizeof(struct list));
tmp->vertex = n;
tmp->next = NULL;
if ((*array)[m] == NULL)
(*array)[m] = tmp;
else {
ptr = (*array)[m];
while(ptr->next != NULL)
ptr = ptr->next;
ptr->next = tmp;
}
}
fclose(luki);
}

关于c - C 函数中的动态结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37194992/

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