Running into error when attempting to printf() different arrays of structures

0

I am trying to print out my 10 different arrays of my struct songList[10]. I have used the debugger and have confirmed each input requested is being stored at each index properly, the issue seems to lie within my printing function. I am getting an "Exception thrown at 0x0003187E in mini1.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC." Error when it tries to loop through at print the strings at the index requested. Any help would be great thanks! Also I am trying to print each songArtist and songTitle starting at index 0 of the songList[10] array. It should print both left aligned and with 35 characters being used for each.

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

#pragma warning(disable:4996)

int getSongInfo(struct songInfo *pFillInfo, char *artistName, char *songName);
int printSongInfo(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(songInfo *songList[])
{
    int counter = 0;

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

    return 1;
}
c
arrays
pointers
struct
printf
asked on Stack Overflow May 16, 2019 by RocktheFries

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0