【发布时间】:2020-05-11 15:33:01
【问题描述】:
我(再次......)在 C 编码项目中苦苦挣扎。
请帮助我了解如何通过动态分配的二维数组访问动态分配的变量。 每次尝试都以失败告终,我找不到正确的语法...
尝试:将字符串放入分配到 **list 中的新 Family 中的 family_name 指针中。
scanf(" %s",&lod->list[0]->family_name);
程序:
#define _CRT_SECURE_NO_WARNINGS
//Libraries
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Consts, globals, structs
#define MAX 10000
typedef struct person
{
char *name;
int gender; //1-female, 0-male
} Person;
typedef struct family
{
char *family_name;
Person *mother, *father; // parents' names
Person **children_list; // list of the children in this family
unsigned child_list_size; // size of child_list array
unsigned current_listChild_size;
} Family;
typedef struct listFamilys
{
Family **list;
unsigned current_listFamily_size; // current size of family
}
ListFamilys;
//main
void main()
{
ListFamilys *lod = (ListFamilys*) malloc(sizeof(ListFamilys));
if(! lod) exit(0);
lod->current_listFamily_size =0;
lod->list = (Family**)malloc(sizeof(Family*));
if (!lod->list) exit(0);
printf("Enter family name: ");
**scanf(" %s",&lod->list[0]->family_name);**
system("pause");
}
【问题讨论】:
-
没有
TownInit函数无法编译代码。 -
函数动态分配ListFamilys,将0放入current_listFamily_size。
-
是的,但您需要实际更新问题中的代码以包含它。
-
完成。提前致谢。