0

I am trying to make a program in order to make playlists for a school assignment.

In order to do this I have made struct for songs and for artist:

struct song {
    int id;     /* unique identity of the song                      */
    char name[40];      /* name of song                             */
    int duration;       /* duration in seconds                          */
    artist* artist;     /* pointer to artist                            */
};

struct artist {
    int id;     /* unique identity of artist                        */
    char name[30];      /* name of artist (could be a band as well)             */
    song* songList;     /* pointer to array containing all songs from this artist   */
    int amountSongs;
};

I then read out the information for this from files in order to get pointerarrays for all the songs and for all the artists.

This all works.

I then go and try to make a playlist, I ask the user to enter the ID of the song that he wants to add to the playlist and retrieve the correct pointer to the song for that:

printf("Type the ID of the song that you want to add\n");
int inputID;
scanf("%i", &inputID);

for(i=0;i<(*numberOfSongs);i++){
    if (inputID==((*song_ptr)+i)->id){
        printf("Song is found!!!!!\n\n");

        songArray  = realloc((songArray ), ((numberSongsInPlaylist) + 1) * sizeof(struct song));
        struct song *next = songArray  + (numberSongsInPlaylist);
        next = ((*song_ptr)+i);
        numberSongsInPlaylist++;

        printf("%i  \n", next->id);
        printf("%i  \n", (*songArray[0]).id);
    }
}

As you can see I print out the the ID numbers over here. Both for the song currently added and for the first song in the songarray. (for debugging) Here is where the problem is.

The first print which prints next->id prints the correct value, the second prints what seems to be an address.

I have tried a couple of things, none of which work which I why I hope someone here can help me with this problem, here are the things that I have tried:

printf("%i  \n", (**songArray[0]).id);     //invalid type unary '*' //does not compile
printf("%i  \n", &(*songArray[0]).id);     //Prints address (I think, value changes with each run)
printf("%i  \n", (*songArray).id);         //Error: request for member 'id' in something not a structure or union
printf("%i  \n", (*songArray)->id);        //Prints address

Any help is more then welcome.

After this I try to add this songarray to a playlist struct that has an ID, this songArray and the amount of songs in it.

I do that in the following code, along with printing the value in the playlist:

*playlist_ptr  = realloc((*playlist_ptr ), ((*numberOfPlaylists) + 1) * sizeof(struct playlist));
struct playlist *nextPlay = *playlist_ptr  + (*numberOfPlaylists);

nextPlay->id = numberOfPlaylists;
nextPlay->numberOfSongs = numberSongsInPlaylist;
nextPlay->songs = songArray;
printf("song ID %d\n\n", ((*playlist_ptr) + 0)->songs[0]->id);

This prints the same value as the second print statement. This is making me wonder if perhaps the problem is in the storing of the data in the songArray, however I could not find the error.


[update from comment]

It [songArray] is actually declared as struct song **songArray = NULL;

1 Answer 1

1

You're really overcomplicating this here. I assume songArray is declared as struct song* songArray? In which case, songArray[0] returns a struct song, not a struct song*. In which case, you just need songArray[0].id.

Sign up to request clarification or add additional context in comments.

2 Comments

It is actually declared as struct song **songArray = NULL; I tried to initialise it as struct song *songArray = NULL; and then do songArray[0].id however this also prints the adress
I actually just checked, making songArray as a pointer and not a point to pointer makes the last print (after saving the playlist in the playlist array) print out a different value all together, while it used to print out the value that (*songArray[0]).id printed out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.