c-存储2d数组的怪异行为



我已经包含了下面的代码,但总的来说,createMonster((函数初始化struct monster字段,然后返回struct指针。**readMonster((函数应该从input.txt文件中读取信息,并返回一个createMonster(((指针数组。例如:monsterList[i]=createMonster(x,y,z(。重申一下,createMonster((保存单个怪物的数据,**readMonsters返回一个带有createMonster数据的怪物列表

问题是,当我使用第一个for循环来循环并用createMonster((数据填充monsterList[I],然后将其打印到屏幕上时,它可以完美地工作。然而,创建一个只打印monsterList[I]的第二个for循环会将奇怪的东西打印到屏幕上。唯一正确打印的是int填充字段。这是下面的代码。

// omitting failed malloc checks for simplicity. Assume no mallocs failed.
#define BUFFERSIZE 50
typedef struct monster
{
char *name;
char *element;
int population;
} monster; // An index
monster *createMonster(char *name, char *element, int population)
{
// Sizeof(struct monster) for clarification.
monster *monster = malloc(sizeof(struct monster));
// Allocating memory for struct monster fields
monster->name = malloc(sizeof(char) * strlen(name) + 1); // +1 null sentinel
monster->element = malloc(sizeof(char) * strlen(element) + 1);
// Initalizing fields based on function input parameters
monster->name = name;
monster->element = element;
monster->population = population;
// Return struct pointer
return monster;
}
monster **readMonsters(FILE *ifp, int *monsterCount)
{
// Initializing array of struct monster pointers
monster **monsterList = NULL;
// Buffer to store monster name and monster element
char name_buffer[BUFFERSIZE], element_buffer[BUFFERSIZE];
// Buffer to store amount of monsters in file
int num_monsters = 0;
// Buffer to store monstor population
int population;
// Iterative variable
int i;
// making monsterCount the address of num_monsters
monsterCount = &num_monsters;
// alloating monsterList and
// simultanenously scaning the value of monsters from the file into the address of num_monsters,
// which is monsterCount
monsterList = malloc(sizeof(int *) * (fscanf(ifp, "%d", monsterCount)) + 1);
// File parsing. Skipping a string based on known input.txt file structure
fscanf(ifp, "%*s");
// storing file information into the monsters' individual monster pointer.
// using num_monsters for clarification and to avoid segmentation faulting
/*  These two for loops are the issue. The first for loop prints everything to
the screen correctly. Therefore, in this for loop, monsterList[I] holds all the
correct createMonster() data. 
The second for loop should just print the same information to the screen, 
but it doesn't. What is going on with malloc?
*/
for (i = 0; i < num_monsters; i++)
{
fscanf(ifp,"%s %s %d", name_buffer, element_buffer, &population);
monsterList[i] = createMonster(name_buffer, element_buffer, population);
// prints correctly
printf("n monsterList[%d]->name: %s, ->element: %s, ->population: %dn", i, monsterList[i]->name, monsterList[i]->element, monsterList[i]->population);
}
for (i = 0; i < num_monsters; i++)
{
// does not print correctly
printf("n monsterList[%d]->name: %s, ->element: %s, ->population: %dn", i, monsterList[i]->name, monsterList[i]->element, monsterList[i]->population);
}
return monsterList;
}

以下是打印到屏幕上的内容:

//这些是正确的

monsterList[0]->名称:圣奥古斯丁,->元素:草,->人口:12

monsterList[1]->名称:结缕,->元素:草,->人口:8

monsterList[2]->名称:全麦,->元素:面包,->人口:6

monsterList[3]->名称:MultiGrain,->元素:面包,->人口:10

monsterList[4]->name:赖,->元素:面包,->人口:10

monsterList[5]->名称:肉桂,->元素:香料,->人口:5

monsterList[6]->名称:胡椒,->元素:香料,->人口:10

monsterList[7]->名称:南瓜,->元素:香料,->人口:30

//(第二个用于循环(这些是不正确的,除了来自细胞1-7 的群体

monsterList[0]->名称:pʛ->元素:pʛ->人口:-641705244

monsterList[1]->名称:南瓜,->元素:香料,->人口:8

monsterList[2]->名称:南瓜,->元素:香料,->人口:6

monsterList[3]->名称:南瓜,->元素:香料,->人口:10

monsterList[4]->名称:南瓜,->元素:香料,->人口:10

monsterList[5]->名称:南瓜,->元素:香料,->人口:5

monsterList[6]->名称:南瓜,->元素:香料,->人口:10

monsterList[7]->名称:南瓜,->元素:香料,->人口:30

对于任何格式错误,我们深表歉意。

行:monsterList=malloc(sizeof(int*(*(fscanf(ifp,"%d",monsterCount((+1(;是个问题。修复:

首先使用fscanf读取怪物的数量,然后使用该值为monsterList 分配内存

最新更新