0

Currently in my program I have the user enter both a Song Title, and Artist Title for some songs. Each string is stored in an array. I am then trying to use those entered strings in a function to allocate memory by using malloc(), then use strcopy to copy the strings into a multi dimensional array variable of my struct. I've copied the code below. I keep getting a "Exception thrown: write access violation." error when my function uses malloc, and cant seem to figure out why. Any help would be appreciated. It is expected that the strings will be no more than than a size of 30, and there will be 10 arrays of the struct variable to store 10 different songs with their artist.

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

#pragma warning(disable:4996)

int getSongInfo(struct songInfo *pFillInfo, char *artistName, char *songName);
int printSongInfo(struct songInfo *songList[]);

struct songInfo {

    char *songArtist;
    char *songTitle;
};

int main(void)
{
    struct songInfo *fillPtr, songList[10];
    fillPtr = &songList[0];

    char tempArtist[30][10];
    char tempSong[30][10];

    int i = 0;
    int counter = 0;
    int arrayCounter = 0;
    while (counter != 10)
    {
        printf("Please enter the artist name: ");
        fgets(tempArtist[counter], sizeof(tempArtist[counter]), stdin);
        printf("Please enter the song name: ");
        fgets(tempSong[counter], sizeof(tempSong[counter]), stdin);

        getSongInfo(&fillPtr[arrayCounter], tempArtist[counter], tempSong[counter]);

        printf("Song and Artist Captured! \n");
        counter++;
        arrayCounter++;
    }

    printSongInfo(&fillPtr);
}

int getSongInfo(struct songInfo *pFillInfo, char *artistName, char *songName)
{
    pFillInfo->songArtist = (char*)malloc(strlen(artistName) + 1);
    pFillInfo->songTitle = (char*)malloc(strlen(songName) + 1);

    strcpy(pFillInfo->songArtist, artistName);
    strcpy(pFillInfo->songTitle, songName);



    return 1;
}

int printSongInfo(struct songInfo *songList[])
{
    int counter = 0;

    while (counter != 10)
    {
        printf("%-35s%-35s", &songList[counter]->songArtist, &songList[counter]->songTitle);
        counter++;
    }

    return 1;
}
RocktheFries
  • 221
  • 2
  • 10
  • 1
    In the `fgets` call, `strlen(tempArtist[counter])` is the length of the string currently in `tempArtist[counter]`. But `tempArtist[counter]` has no defined contents, because you have not initialized it. You want to tell `fgets` how much memory is available there, not what the length of the string currently there is. Use `sizeof tempArtist[counter]`. – Eric Postpischil May 15 '19 at 23:10
  • 1
    `int getSongInfo(struct songInfo *pFillInfo, char artistName, char songName)` passes only a single character for `artistName` and a single character for `songName`. These characters are passed by value, so the addresses of the characters in the `main` routine are not passed. When `getSongInfo` uses `&artistName`, it gets the address of its parameter, which is not what you want. Change the function declaration to `int getSongInfo(struct songInfo *pFillInfo, char *artistName, char *songName)`. This will require corresponding changes in the function body and in `main`. – Eric Postpischil May 15 '19 at 23:13
  • 1
    In `getSongInfo`, do not free the memory you just allocated. This memory should be retained until the data it contains is no longer needed. It would be freed by a separate routine called when the program is disposing of the saved data (or could be left to be reclaimed by the operating system when the program exits). – Eric Postpischil May 15 '19 at 23:14
  • @EricPostpischil I have updated the code to reflect these changes, is there a reason why it is only saving the first entry to fillPtr? It wont record the following 9. Thanks! – RocktheFries May 16 '19 at 00:40
  • @EricPostpischil I double checked index 0-10 through the array and I was wrong, it is storing all 10, but I'm having issues printing the struct, maybe you have some insight, the function is added to the code above, thanks! – RocktheFries May 16 '19 at 00:55

0 Answers0