如何动态分配结构数组?

时间:2012-03-31 19:50:52

标签: c

如何为曲目动态分配结构数组,以便我可以使用all_albums_p[i]访问它?我需要all_albums_p [i]才能访问它,因为我正在返回all_albums_p。

struct tracks_{
  char tracks_title;
  int playlist_hits;
};

struct album_ {
  int num_tracks;
  struct tracks_ tracks;
};

typedef struct album_ album;
typedef struct tracks_ tracks;

album *all_albums_p = (album *)malloc(sizeof(album)*number_of_album);

** * ** * ** 我是什么已更改 * ** * ****

struct tracks_{
      char *tracks_title;
      int *playlist_hits;
    };

3 个答案:

答案 0 :(得分:1)

现在,你的每张专辑都有一首曲目,曲目名称应该是一个字符,而不是你想要的字符数组。

您已经正确分配了相册,但您也需要分配每张专辑的曲目。

最简单的方法是只指定固定数量的曲目作为最大值,并确保其他代码不超过该值。

const int MAX_TRACKS = 20;
const int MAX_HITS = 20;
const int MAX_TRACK_NAME_LENGTH = 63;

struct track_{
    char tracks_title[MAX_TRACK_NAME_LENGTH+1];
    int playlist_hits[MAX_HITS];
    int playlist_hits_count;
};

struct album_ {
    int num_tracks;
    struct track_ tracks[MAX_TRACKS];
};

或者,如果您有方便的信息,您可以动态地将曲目和曲目名称分配到所需的确切大小。例如,假设您只是复制现有的专辑数组:

struct track_ {
    char *track_title;
    int *playlist_hits;
    int playlist_hits_count;
};

struct album_ {
    int num_tracks;
    struct track_ *tracks;
};

typedef struct album_ album;
typedef struct track_ track;

album *copy_albums(album *all_albums_p, int number_of_album) {
    album *all_albums_copy = (album *)malloc(sizeof(album) * number_of_album);

    // copy each album
    for (int album_i = 0; album_i < number_of_album; album_i++) {
        album * current_album = all_albums_p + album_i;
        album * copy_album = all_albums_copy + album_i;

        copy_album->num_tracks = current_album->num_tracks;
        copy_album->tracks = (track *)malloc(sizeof(track) * current_album->num_tracks);

        // copy each track, and it's hits, and make a new copy of it's name
        for (int track_i = 0; track_i < current_album->num_tracks; track_i++) {
            track * current_track = current_album->tracks + track_i;
            track * copy_track = copy_album->tracks + track_i;

            copy_track->playlist_hits_count = current_track->playlist_hits_count;
            copy_track->playlist_hits = (int *)malloc(sizeof(int) * current_track->playlist_hits_count);
            memcpy(copy_track->playlist_hits, current_track->playlist_hits, current_track->playlist_hits_count * sizeof(int));

            copy_track->track_title = _strdup(current_track->track_title);
        }
    }

    return all_albums_copy;
}

答案 1 :(得分:0)

typedef struct tracks_{
  char tracks_title[101];
  int playlist_hits;
} tracks;

typedef struct album_ {
  int num_tracks;
  struct tracks_ tracks[20];
} album;

album *all_albums_p = (album *)malloc(sizeof(album)*number_of_album);

会将number_of_albums次分配给每张专辑固定最多20首曲目的相册。

答案 2 :(得分:0)

你有一些选择。

  1. 修复标题的最大长度字符串与标题的动态字符串:

    enum { MAX_TITLE_LENGTH = 32 };
    typedef struct tracks
    {
      char tracks_title[MAX_TITLE_LENGTH];
      int  playlist_hits;
    } tracks;
    

    VS

    typedef struct tracks
    {
      char *tracks_title;
      int   playlist_hits;
    } tracks;
    

    第一个使得音轨分配更简单,并且发布更简单,但如果找到的音轨标题超过最大值,则会遇到麻烦。如果你有很多短道标题,它也可能比其他选择浪费更多的空间。

  2. 对于专辑,您需要为每个专辑使用固定大小的曲目阵列或指针或“灵活的阵列成员”(或者,如果您坚持使用1999年之前的编译器,那么可能需要使用'struct hack')。

    enum { MAX_TRACKS_PER_ALBUM = 32 };
    typedef struct album
    {
      int     num_tracks;
      tracks  tracks[MAX_TRACKS_PER_ALBUM];
    } album;
    

    VS

    typedef struct album
    {
      int     num_tracks;
      tracks *tracks;
    } album;
    

    VS

    typedef struct album
    {
      int     num_tracks;
      tracks  tracks[];
    } album;
    

    只要您不想要它们的数组,最后一个使用起来非常简单。 (你可以使用灵活数组成员的专辑指针数组。)鉴于你想要一个专辑数组,你可能最好使用指针版本:

    album *all_albums = (album *)malloc(sizeof(album) * number_of_albums);
    

    但是您需要初始化相册结构(因为malloc()不会这样做),并且您需要为每张专辑分配适当数量的曲目。