Reputation: 689
How can I access a array of structures from another structures? I would like to access track_titles from all_albums_p. I tried all_albums_p[i] -> all_tracks_p[j].tracks_title
but it gives me an error
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);
fscanf(album_file,"%d", &all_albums_p[j].num_tracks);
tracks *all_tracks_p = (tracks *)malloc(sizeof(tracks)*all_albums_p[j].num_tracks);
for(i=0;i<all_albums_p[j].num_tracks;i++){
fscanf(album_file,"%d", &number_of_charaters);
all_tracks_p[i].tracks_title = (char *)malloc(sizeof(char)*(number_of_charaters+1));
fscanf(album_file, "%[^\n]s", all_tracks_p[i].tracks_title);
printf("%s\n",all_tracks_p[i].tracks_title);
all_tracks_p[i].playlist_hits = 0;
}
/*These is where it is giving me errors*/
for(i=0; i < 10 ;i++){
printf("%d : %d\n",i,all_tracks_ptr[i].num_tracks);
for(j=0; j < number_of_albums[i].num_tracks ;j++){
printf("%s", number_of_albums[i]->all_tracks[j].tracks_title)
}
}
The compiler is saying:
project3.c:26:39: error: request for member 'num_tracks' in something not a structure or union
project3.c:27:34: error: invalid type argument of '->' (have 'int')
Upvotes: 1
Views: 90
Reputation: 754545
As you've declared it now you've only associated one track per album. I believe you want to declare multiple tracks per item so you should make it a pointer type
struct album_ {
int num_tracks;
struct tracks_* tracks;
};
Once you've done that then having an all_tracks
variable doesn't really make sense. Tracks are associated with albums so you want to allocate them within each album instead of globally. Since this is homework I don't want to give the exact answer but the trick is to initialize the albums one at a time and add tracks as a group to each album.
Upvotes: 2