0

I am trying to write program which takes in song names, genres, and artists. The code to input these is this:

printf("Genre: ");
getStringFromUserInput(input, MAX_LENGTH);
length = strlen(input);
genre = (char*)malloc(length);
strcpy(genre, input);
getStringFromUserInput(input, MAX_LENGTH);

I have a function to initialize a new node, but when building the program it is giving me the warning.

Node *newNode(char songName, char artist, char genre, Node *link){
    Node *new;
    new = (Node *)malloc(sizeof(Node)); //dynamic memory allocation of 1 node
    if(new!=NULL){
        //if there is memory, inputs data into bucket
        new->songName = songName;
        new->artist = artist;
        new->genre = genre;
        new->link = link;
    }
    return (new);
}

Any help would be great, thanks!

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 2
    [Don't cast malloc in C](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Barmar Mar 29 '17 at 21:06
  • 4
    `char songName` means the song's name is just one character. Don't you mean `char *songName` and the same for `artist` and `genre`? – Barmar Mar 29 '17 at 21:09
  • yes Thank you! not sure how I missed that. – Naveed Farahani Mar 29 '17 at 21:14
  • 2
    `length = strlen(input); genre = malloc(length); strcpy(genre, input);` You're copying one more character than you allocated. Don't forget the terminating `'\0'`. – aschepler Mar 29 '17 at 21:15

0 Answers0