I can't figure out why I am getting an error telling me that my pointer to a char in a struct is not initialized:
Here is the struct and function that is getting the error.
Code:
typedef struct song_info
{
char *artist;
char *album;
char *title;
char *genre;
struct song_length length;
int num_plays;
int rating;
} Song_struct;
Song_struct *getInfo(void)
{
Song_struct *newSong;
Song_struct *pMem;
char artist[32], album[32], title[32], genre[32];
int min, sec, num_played, rating;
char filler;
printf("Please enter song information...\n");
printf("Artist: ");
scanf("%c");
gets(artist);
printf("Album: ");
gets(album);
printf("%s", album);
printf("Song Title: ");
gets(title);
printf("Genre: ");
gets(genre);
printf("Song length (min:sec): ");
scanf("%d%c%d", &min, &filler, &sec);
printf("Number of times played: ");
scanf("%d", &num_played);
printf("Rating: ");
scanf("%d", &rating);
newSong->artist = (char *)malloc(sizeof(char)*strlen(artist));
newSong->album = (char *)malloc(sizeof(char)*strlen(album));
newSong->title = (char *)malloc(sizeof(char)*strlen(title));
newSong->genre = (char *)malloc(sizeof(char)*strlen(genre));
newSong->length.minutes = (int)malloc(sizeof(int));
newSong->length.seconds = (int)malloc(sizeof(int));
newSong->num_plays = (int)malloc(sizeof(num_played));
newSong->rating = (int)malloc(sizeof(rating));
strcpy(newSong->artist, artist);
strcpy(newSong->album, album);
strcpy(newSong->title, title);
strcpy(newSong->genre, genre);
newSong->length.minutes = min;
newSong->length.seconds = sec;
newSong->num_plays = num_played;
newSong->rating = rating;
return newSong;
}
The program crashes right when the first newSong->artist = ... call to malloc is started.