[英]Dynamic 2d array inside struct in C
At first I'd like to say that despite experience with other programming lanugages, I'm new to C and I'm trying my best to understand all of its concepts.首先我想说的是,尽管有其他编程语言的经验,但我是 C 的新手,我正在尽力理解它的所有概念。 I know that dynamic allocation and especially 2d arrays inside structures aren't things that C is good at, but regardless of that I would like to understand how it functions.
我知道动态分配,尤其是结构内部的 2d arrays 并不是 C 擅长的事情,但无论如何我都想了解它的功能。
First of all the reason why I'm trying to use a dynamic 2d array instead of two 1d arrays is because I'd like to benefit from the foo[x][y] syntax.首先,我尝试使用动态二维数组而不是两个一维 arrays 的原因是因为我想从 foo[x][y] 语法中受益。 Also array foo[x][y] should change it's size according to what's passed to function that allocates memory for the enitre struct.
此外,数组 foo[x][y] 应该根据传递给为整个结构分配 memory 的 function 的内容来更改其大小。
My problem is that I cannot seem to correctly allocate enough memory for array inside a Bus
struct.我的问题是我似乎无法为
Bus
结构内的数组正确分配足够的 memory。 I've been looking around web but I cannot seem to find a working example of such allocation and adapt it to my example.我一直在寻找 web 但我似乎找不到这种分配的工作示例并将其调整为我的示例。 What I've tried so far was: declaring flexible array memeber inside struct and allocating it with 'make_structure' function, to later access it with macro.
到目前为止,我尝试过的是:在 struct 中声明灵活的数组成员并使用 'make_structure' function 分配它,以便稍后使用宏访问它。
Current example has the "pointer to a pointer" way of declaring dynamic 2d array.当前示例具有声明动态二维数组的“指向指针的指针”方式。 The program crashes when it's ran.
程序运行时崩溃。 Functions:
功能:
struct Bus *head;
struct Bus *current;
struct Bus *new;
struct Bus *make_structure(int r, int s);
void test() {
int i, NUM;
srand(time(NULL));
head = make_structure(ROW_SIZE, SEAT_AMOUNT);
current=head;
random_fill_structure(current);
current->next=NULL;
current=head;
show_structure();
}
struct Bus *make_structure(int r, int s) {
int i;
struct Bus* a;
a->seats = (int**)calloc(r,sizeof(int*));
for (i=0;i<r; i++) {
a->seats[i] = (int*)calloc(s,sizeof(int));
}
if(a==NULL)
{
puts("Error");
exit(1);
}
return(a);
}
struct Bus {
char name[20];
int **seats;
struct Bus *next;
};
The other two functions present in driver function random_fill_structure
and show_structure
don't do anything with the seats**
array so they shouldn't be the case of programme crashing.驱动程序 function
random_fill_structure
和show_structure
中的其他两个函数对seats**
数组没有任何作用,因此它们不应该是程序崩溃的情况。
Edit 1: I've allocated memory for the struct in make_structure
function with this line of a=(struct Bus *)malloc(sizeof(struct Bus));
编辑 1:我已经为 make_structure function 中的结构分配了
make_structure
,这行a=(struct Bus *)malloc(sizeof(struct Bus));
and the programme doesn't crash anymore.并且程序不再崩溃。 Though whenever I'd try to access it inside
show_structure
like so:尽管每当我尝试像这样在
show_structure
中访问它时:
for (i = 0; i < ROW_SIZE; i++) {
for (j = 0; j < SEAT_AMOUNT; j++) {
printf("[%d]", a->seats[i][j]);
}
}
The app will crash as it tries to print the first value of 2d array.该应用程序将在尝试打印 2d 数组的第一个值时崩溃。
Edit 2: Function used to print the structure:编辑2: Function 用于打印结构:
void show_structure() {
int i, j;
struct Bus *ptr = head;
while(ptr != NULL) {
printf("%s slots: ",ptr->name);
printf("\n");
ptr = ptr->next;
for (i = 0; i < ROW_SIZE; i++) {
for (j = 0; j < SEAT_AMOUNT; j++) {
printf("[%d]", ptr->seats[i][j]);
}
}
}
}
You shoul allocate memory for 'a' before accessing 'a->seats'.在访问“a->seats”之前,您应该为“a”分配 memory。
struct Bus *a = malloc(sizeof *a);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.