C函数中的动态结构数组
Dynamic struct array in function in C
我正在尝试创建邻接表来表示文件 "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, array);
#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);
}
我正在尝试创建邻接表来表示文件 "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, array);
#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);
}