简体   繁体   English

具有结构的动态内存分配

[英]Dynamic memory allocation with structures

The code down below is suppose to read and store an input-ed text file and store its contents in the structure appropriately, dynamically. 下面的代码假设是读取和存储输入的文本文件,并将其内容适当地动态动态存储在结构中。 The first number is number of albums, second is number of tracks within this album, and the number in front of each title is the number of characters in that title. 第一个数字是专辑的数量,第二个数字是此专辑中的曲目数量,每个标题前面的数字是该标题中的字符数量。

For the code I have written so far, I am trying to make it so that each album correlates to 1 independent structure each, so I created a Struct array. 对于到目前为止我写的代码,我试图使其每张专辑都与1个独立结构相关,因此我创建了一个Struct数组。 So currently right now I am able to read in the number of albums and number of tracks correctly. 因此,目前我可以正确读取专辑数量和曲目数量。 However, I am having trouble with the pointer array in struct album . 但是,我在struct album的指针数组时遇到了麻烦。 I am having trouble both dynamically allocating the correct memory for it, as well as storing all (in this case 17) track names into the entire pointer array, without the numbers in front of the track titles. 我既无法为其动态分配正确的内存,又无法将所有(在本例中为17个)轨道名称存储到整个指针数组中,而轨道标题前面没有数字,这给我带来麻烦。

Example Text File: 示例文本文件:

1
17
16 Like an umbrella
...
15 Dynasty Warrior

Code: 码:

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

struct album 
{
     int num_tracks;
     char *tracks[];

};

int main(int argc, char *argv[]){

int numbALBUMS=0, numbCharInTrack=0;
int i=0,j=0;

    FILE *albums;
    albums = fopen (argv[1], "r");

    fscanf(albums, "%d", &numbALBUMS);
    struct album info[numbALBUMS];

    for(i=0;i<numbALBUMS;i++){
        fscanf(albums, "%d", &info[i].num_tracks);
            for(j=0;j<info[i].num_tracks;j++){
                fscanf(albums, "%d" , &numbCharInTrack);

                //NEED HELP HERE
                //fscanf(albums,"s",info[i].tracks[j]);
            }

    }

fclose(albums);
return 0;

}
struct Album info[numAlbums]

is incorrect, you have to do something like this : 是不正确的,您必须执行以下操作:

struct Album *info = (struct album*)malloc(numAlbums * sizeof(struct Album));

A statement of the form type name[numOfElements]; 格式type name[numOfElements];语句type name[numOfElements]; can only be used for static allocation. 只能用于静态分配。

You should replace char *tracks[] with char **tracks . 您应该将char *tracks[]替换为char **tracks You can use char *tracks[], but this is more difficult.(if you want to have a look a it : What's the need of array with zero elements? ) 您可以使用char * tracks [],但这会更加困难。(如果您想看看它: 零元素数组的需求是什么?

You then have to add 然后,您必须添加

info[i].tracks = malloc(sizeof(char*) * info[i].num_tracks);

in the first for loop after the fscanf statement. fscanf语句之后的第一个for循环中。

To end, you have to allocate in the innermost for loop, after the first fscanf statement, memory for the string that holds the trak name. 最后,您必须在第一个fscanf语句之后的最里面的for循环中分配用于存储trak名称的字符串的内存。 You can do that like this : 您可以这样:

info[i].tracks[j] = malloc(sizeof(char) * (numbCharInTrack + 1));  //number of characters + '\0'

Then you can read the track name from the file. 然后,您可以从文件中读取曲目名称。

fgetc(album); //Read past the leading space
fgets(info[i].tracks[j], numbCharInTrack + 1, album);

Edit 编辑

Added read name from file code. 从文件代码添加了读取名称。

Replaced malloc(sizeof(char) * numbCharInTrack) with malloc(sizeof(char) * (numbCharInTrack + 1)) , forgot terminating null character. malloc(sizeof(char) * (numbCharInTrack + 1))替换了malloc(sizeof(char) * numbCharInTrack) malloc(sizeof(char) * (numbCharInTrack + 1)) ,忘记了终止空字符。

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

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