Thread: Initialization Error..? Help Please

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    24

    Initialization Error..? Help Please

    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.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Where do you allocate space for "newSong" or set it to a valid address?

    Once you fix that remember you need to allocate space for the /0 at end of string.
    Code:
    malloc(sizeof(char)*(strlen(artist)+1));
    Tim S.
    Last edited by stahta01; 01-31-2012 at 06:35 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    Oh thanks for the reminder, but I didn't know I have to allocate memory for newSong, I thought you only have to allocate memory for the data that is encapsulated within newSong. so to do that do I use this:
    Code:
    newSong = (newSong *)malloc(sizeof(newSong));
    thanks

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Arg! Why, oh why are you using gets()??? Stop immediately!

    You also should not be casting the return value of malloc. If you have to because of warnings or errors, it's because you're either compiling as C++ or you've forgotten to #include <stdlib.h>

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Neither of the structs are being allocated, and pMem doesn't seem to be used. You don't need to allocate non-pointer variables, such as ints.

    Why %c for artist? That's incorrect, it's %s like everything else.

    Don't use gets(), and while you're at it, get rid of the temporary variables and just fill in the struct.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    what should I use instead of gets() than? If I use scanf then I can't use more than one word because the scanf stops at whitespace.

    Also, memcpy, if I take out the scanf("%c"); after printf("Artist: "); then the program skips the gets() for artist and goes straight to the next printf("Album: "); without stopping to take in the string.
    Last edited by Matt Hintzke; 01-31-2012 at 08:00 PM.

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Use fgets(), it removes all the issues with whitespace and newlines (which lets you remove the scanf %c), and you can specify the length.

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    ok thanks a lot

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Note, fgets will leave the new line in the buffer, so you have to remove that by hand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array initialization error
    By FlyingShoes12 in forum C++ Programming
    Replies: 3
    Last Post: 11-14-2009, 11:07 AM
  2. compile error about member variable initialization
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 12-04-2007, 11:55 AM
  3. SDL Initialization Error...
    By Comrade_Yeti in forum C++ Programming
    Replies: 0
    Last Post: 05-16-2005, 05:43 PM
  4. variable initialization error...
    By sunoflight77 in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2005, 12:01 AM
  5. Error: initialization of non-const reference type
    By JerryL in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2004, 07:43 PM