简体   繁体   English

从指向 C 中结构的指针数组中获取信息

[英]Getting information from an array of pointers to structs in C

So i'm writing this code for an assignment for school, and what I have to do is have two functions as they're written, with createMonster returning a dynamically allocated monster with data, and readMonsters returning an array of pointers to the monsters made in createMonster .所以我正在为学校的作业编写这段代码,我必须做的是在编写它们时有两个函数, createMonster返回一个带有数据的动态分配的怪物,而readMonsters返回一个指向怪物的指针数组在createMonster I've been having trouble understanding how to get the functions and structs to cooperate, and this is what I have so far:我一直无法理解如何让函数和结构合作,这就是我到目前为止所拥有的:

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

typedef struct monster {
    int id;
    char *name;
    char *element;
    int population;
} monster;

monster* createMonster(char *name, char *element, int population)
{
    monster *amonster = (monster*)malloc(sizeof(monster));
    amonster->name = (char*)malloc(sizeof(char)*sizeof(name));
    amonster->element = (char*)malloc(sizeof(char)*sizeof(element));
    amonster->population = (int)malloc(sizeof(int)*sizeof(population));
    amonster->name = name;
    amonster->element = element;
    amonster->population = population;
    return amonster;
}

monster** readMonsters(FILE* infile, int *monsterCount)
{
    char n[15], e[15];
    int p;
    monster **a_array = malloc(*monsterCount * sizeof(struct a*));
    for (int i = 0; i < *monsterCount; i++) {
        
        a_array[i] = malloc(sizeof(monster));
        
        fscanf(infile,"%s %s %d",n, e, &p);
        printf("%s %s %d\n", n, e, p);
        a_array[i] = createMonster(n,e,p);
       
    }
    monster ***m = &a_array;
    return *m;
}

int main(){
    int monsterCount;
    char name[15];
    
    
    FILE *fp = fopen ( "in.txt", "r" );
    fscanf(fp,"%d %s",&monsterCount,name);
    //printf("test %d\n",monsterCount);
    monster **mptr = readMonsters(fp,&monsterCount);
    
    printf("%s %s %d\n", (mptr)[3]->name,(mptr)[3]->element,(mptr)[3]->population);
    
    fclose(fp);
    return 0;
}

With the input file being:输入文件为:

8 monsters
StAugustine Grass 12
Zoysia Grass 8
WholeWheat Bread 6
MultiGrain Bread 10
Rye Bread 10
Cinnamon Spice 5
Pepper Spice 10
Pumpkin Spice 30

However, when I run it, I can see it works as far as making them the first time, but when I try and go back to access the data it returns garbage for the strings, with this being my output:但是,当我运行它时,我可以看到它在第一次制作它们时有效,但是当我尝试返回访问数据时,它返回字符串的垃圾,这是我的输出:

StAugustine Grass 12
Zoysia Grass 8
WholeWheat Bread 6
MultiGrain Bread 10
Rye Bread 10
Cinnamon Spice 5
Pepper Spice 10
Pumpkin Spice 30
`7 w├?]╨@ 10
   w├?]╨@ 10

I've tried rearranging the functions, pointers, etc. and have tried many different versions of these functions trying to follow guides online, but each time it either doesn't work at all or returns garbage.我尝试重新排列函数、指针等,并尝试了这些函数的许多不同版本,试图按照在线指南进行操作,但每次它要么根本不工作,要么返回垃圾。 I'm looking for any help in understanding how to get this working or how it could be organized better since I will readily admit my experience with C is moderate at best.我正在寻找任何帮助来理解如何使其工作或如何更好地组织它,因为我很容易承认我对 C 的经验充其量是中等的。

The code you posted has the following issues:您发布的代码存在以下问题:

The line线

amonster->name = (char*)malloc(sizeof(char)*sizeof(name));

does not make sense.没有意义。 sizeof(name) is the size of the pointer, which is 32-bit or 64-bit, depending on your platform. sizeof(name)是指针的大小,它是 32 位还是 64 位,具体取决于您的平台。 However, you probably need to allocate more than that.但是,您可能需要分配更多。 You need to allocate strlen(name) + 1 bytes, because that is the length of the string that is passed to the function (including the null terminating character).您需要分配strlen(name) + 1个字节,因为这是传递给函数的字符串的长度(包括空终止字符)。 The same also applies to the next line:这同样适用于下一行:

amonster->element = (char*)malloc(sizeof(char)*sizeof(element));

Also, the line此外,该行

amonster->population = (int)malloc(sizeof(int)*sizeof(population));

does not make sense, because amonster->population is not a pointer.没有意义,因为amonster->population不是指针。 Why are you trying to store a pointer to dynamically allocated memory in it?为什么要在其中存储指向动态分配内存的指针? You shouldn't need dynamic memory allocation here, because amonster->population is not a string, but a fixed-length variables, for which you have already allocated space, because it is part of the struct .你不应该在这里需要动态内存分配,因为amonster->population不是一个字符串,而是一个固定长度的变量,你已经为其分配了空间,因为它是struct一部分。 Therefore, you can delete this line.因此,您可以删除此行。 All you need is the line amonster->population = population;你只需要一行amonster->population = population; , which you already have. ,您已经拥有。

Additionally, the line此外,该行

amonster->name = name;

does not do what you want.不做你想做的。 It does not copy the string, it only copies the pointer, ie the memory address.它不复制字符串,它只复制指针,即内存地址。 That way, you are copying a pointer which will be a dangling pointer by the time you return to the function main .这样,当您返回到函数main时,您正在复制一个指针,该指针将成为一个悬空指针 In order to copy the actual string, you must write strcpy( amonster->name, name );为了复制实际的字符串,您必须编写strcpy( amonster->name, name ); . . The same applies for the following line:这同样适用于以下行:

amonster->element = element;

The line线

a_array[i] = malloc(sizeof(monster));

is unnecessary and only creates a memory leak .是不必要的,只会造成内存泄漏 You have already allocated space for all structs in the function createMonster .您已经为函数createMonster中的所有结构分配了空间。

The lines线条

monster ***m = &a_array;
return *m;

are unnecessarily cumbersome and can be simplified to the following:不必要的麻烦,可以简化为以下内容:

return a_array;

After applying all of these fixes, your code should look like this:应用所有这些修复后,您的代码应如下所示:

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

typedef struct monster {
    int id;
    char *name;
    char *element;
    int population;
} monster;

monster* createMonster(char *name, char *element, int population)
{
    monster *amonster = (monster*)malloc(sizeof(monster));
    amonster->name = (char*)malloc(sizeof(char)*(strlen(name)+1)); //fixed
    amonster->element = (char*)malloc(sizeof(char)*(strlen(element)+1)); //fixed
    //amonster->population = (int)malloc(sizeof(int)*sizeof(population)); //redundant
    strcpy( amonster->name, name ); //fixed
    strcpy( amonster->element, element ); //fixed
    amonster->population = population;
    return amonster;
}

monster** readMonsters(FILE* infile, int *monsterCount)
{
    char n[15], e[15];
    int p;
    monster **a_array = malloc(*monsterCount * sizeof(struct a*)); //what is struct a???
    for (int i = 0; i < *monsterCount; i++) {
        
        //a_array[i] = malloc(sizeof(monster)); //redundant
        
        fscanf(infile,"%s %s %d",n, e, &p);
        printf("%s %s %d\n", n, e, p);
        a_array[i] = createMonster(n,e,p);
       
    }
    //monster ***m = &a_array; //removed
    //return *m; //removed
    return a_array;
}

int main(){
    int monsterCount;
    char name[15];
    
    FILE *fp = fopen ( "in.txt", "r" );
    fscanf(fp,"%d %s",&monsterCount,name);
    //printf("test %d\n",monsterCount);
    monster **mptr = readMonsters(fp,&monsterCount);
    
    printf("%s %s %d\n", (mptr)[3]->name,(mptr)[3]->element,(mptr)[3]->population);
    
    fclose(fp);
    return 0;
}

As demonstrated here , your program now provides the correct output.这表现在这里,你的程序现在提供正确的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM