Thread: Data Structures Warning

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    36

    Data Structures Warning

    I am getting a warning on the program I am coding and I dont know what the reason is.
    Code:
    // assigns a data type String
    typedef char String[STRINGSIZE];
    
    // Assigns a data structure for file contents
    typedef struct CDInfo		
    {
    	String artist;
    	String album;
    	int numTracks;
    } CDInfo;
    Code:
    // FUNCTION PROTOTYPE
    void ReadMusicData(CDInfo);
    Code:
    // MUSIC FILE READING FUNCTION CALL
    ReadMusicData(info); //error here

    Code:
    // MUSIC FILE READING FUNCTION
    void ReadMusicData(CDInfo info)   
    {
    // read in the title, author and then strip EOLNS 
     fgets ( info.artist, STRINGSIZE, inFile );
     fgets ( info.album, STRINGSIZE, inFile );
     info.artist [strlen (info.artist) - 1] = '\0';
     info.album [strlen (info.album) - 1] = '\0';
     
     fscanf(inFile, "%d", &info.numTracks);
     
     printf("Artist: %s\n", info.artist);
     printf("Album: %s\n", info.album);
     printf("Number of tracks: %d\n", &info.numTracks);
    }
    Heres the warning:
    c:\program files\microsoft visual studio\myprojects\music2\music2.c(106) : warning C4700: local variable 'info' used without having been initialized

    I dont know how to initialize a struct but I did not think I had too. Have I missed something?

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    USA
    Posts
    29
    I believe you are passing a variable that hasn't been created.

    Basically...

    Code:
    // MUSIC FILE READING FUNCTION CALL
    ReadMusicData(info); //error here
    would at least need to be...

    Code:
    // MUSIC FILE READING FUNCTION CALL
    CDInfo info;
    ReadMusicData(info); //error here
    but I think you have more problems than that in your code... because your 'info' variable wouldn't actually be populated after the 'ReadMusicData' function completes. This is because the 'ReadMusicData' function will actually read the data into a local copy of 'info' that doesn't get returned to outside the function.

    You have two options...

    1) Pass in a pointer to the ReadMusicData function

    or

    2) Return the populated CDInfo from the ReadMusicData function...

    i.e. call the function in the following way...

    Code:
    CDInfo info = ReadMusicData();
    Of course, you would have to update your ReadMusicData function as necessary.

    Hope that helps.

    Eddie

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    36
    I declared my
    Code:
    CDInfo info;
    in main.
    I thought that structs did not need to be passed as pointers because they...were pointers. Maybe thats for an array of structs...

    This does look like my problem though. Can someone show me how to pass the struct to a function using pointers?

    I would think that it was like this:
    declare
    Code:
    void ReadMusicData(CDInfo*);
    call
    Code:
    ReadMusicData(&info);
    definition
    Code:
    void ReadMusicData(CDInfo* info)
    But I am getting this error now when I try to store variables.
    C:\Program Files\Microsoft Visual Studio\MyProjects\Music2\music2.c(224) : error C2231: '.album' : left operand points to 'struct', use '->'

    Never seen a "->" before what is it?
    Last edited by jlharrison; 04-07-2006 at 12:11 AM.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    void ReadMusicData(CDInfo* p);

    to access struct members using p.
    p->artist;
    p->album;
    and so on.


    Code:
    void ReadMusicData(CDInfo info)   
    {
    // read in the title, author and then strip EOLNS 
     fgets ( info.artist, STRINGSIZE, inFile );
     fgets ( info.album, STRINGSIZE, inFile );
     info.artist [strlen (info.artist) - 1] = '\0';
     info.album [strlen (info.album) - 1] = '\0';
     
     fscanf(inFile, "%d", &info.numTracks);
     
     printf("Artist: %s\n", info.artist);
     printf("Album: %s\n", info.album);
     printf("Number of tracks: %d\n", &info.numTracks);
    }
    >printf("Number of tracks: %d\n", &info.numTracks);
    &info.numTracks?

    inFile needs to be defined.

    Code:
    ReadMusicData(info);
    this is not needed.
    Last edited by qqqqxxxx; 04-07-2006 at 12:35 AM.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    36
    This is what I have.

    Code:
    #include <stdio.h>  // includes standard in out library
    #include <stdlib.h> // includes standard library
    #include <time.h>   // includes system time library
    #include <string.h> // includes the string library
    
    #define ARRAYSIZE 15 // constant for the array size
    #define BLANK ' '    // constant for blanks
    #define QUITCHOICE 4 // constant to quit
    #define STRINGSIZE 50 //constant for string sizes
    
    // DATA TYPES
    // assigns a data type cdName
    typedef char CDName;
    
    // assigns a data type cdName
    typedef char String[STRINGSIZE];
    
    // Assigns a data structure for file contents
    typedef struct CDInfo		
    {
    	String artist;
    	String album;
    	int numTracks;
    } CDInfo;
    
    
    // FUNCTION PROTOTYPES
    void PrintMenu(int*);
    
    void ReadMusicData(CDInfo*);
    
    void PlayInOrder( );
    
    void PlayRandomOrder( );
    
    void PlayOneTrack( ); 
    
    void PrintClosing( );
    
    /*==========================================================================*/
    /* MAIN FUNCTION */
    
    int main()
    { 
     // data type declarations for program arrays
     CDInfo info;
    
      // integer variable for menu choice
     int menuChoice = 0;
    
     PrintMenu(&menuChoice);
    
     // Error checks for 4
     while (menuChoice != 4)
    	{
    	// Call to function that reads in the array
    	ReadMusicData(&info);
    	// Switch to process user input
    	switch (menuChoice)
    			{
    			// if 1 call to Play Function
    			case 1: //PlayInOrder( );
    					break;
    			// If 2 call to Play Random Fuction
    			case 2: //PlayRandomOrder( );
    					break;
    			// If 3 call to Play One Fuction
    			case 3: //PlayOneTrack( );
    					break;
    			}
    	// Call to print menu function
    	PrintMenu(&menuChoice);
    
    	} 
     // Checks for a 4 to quit
     while (menuChoice == QUITCHOICE)
    	{
    	 // Call to print closing function
    	 PrintClosing();
    	 // reset the menu choice to exit loop
    	 menuChoice = 0;
    	}
     // end of main returning 0 
     return 0;
    }
    
    /* ========================================================================== */
    /* MENU PRINTING FUNCTION */
    
    // This function recieves a variable integer from main and returns it by 
    // simulated reference using pointers. The value is then processed by main
    // to determine what the user wants to do.
    void PrintMenu(int *menuChoice)
    {
     // prints message
     printf ("\n************************************************************\n"
              "            WELCOME TO THE JLH05G MUSIC PLAYER!\n"          
             "************************************************************\n\n");
     // prints menu
     printf ("Please Choose a Menu Choice 1, 2, 3, or 4.\n"
    	     "Then You Will Be Asked What File To Open.\n"
    		 "1: Play Current Playlist In Order\n"
             "2: Play Randomized Playlist\n" 
             "3: Play One Song\n" 
             "4: Leave the Program\n");
     
     // inputs menu choice
     scanf("%d", menuChoice);
     // makes sure menu choice is valid
     while ((*menuChoice < 1) || (*menuChoice > 4))
    	{
    	// prints menu
    	printf ("\nInvalid Menu Choice!\n"
    		    "Please Choose a Menu Choice 1, 2, 3, or 4\n"
    			"1: Play Current Playlist In Order\n"
    			"2: Play Randomized Playlist\n" 
    			"3: Play One Song\n" 
    			"4: Exit the Program\n");
        // re inputs value
    	scanf("%d", menuChoice);
    	} 
     // echo prints users input
     printf("You chose %d\n\n", *menuChoice);
     // end of function return to main 
     return;
    }// Function PrintMenu
    
    
    /*==========================================================================*/
    /* MUSIC FILE READING FUNCTION */
    
    // This function reads from the file in to the array playList then reads from
    // the file in to the typeCode to process what to store in the second array.
    // It returns the arrays to main by simulated reference.
    void ReadMusicData(CDInfo* info)  
    {
    // int lcv;
      // type for holding the file name
     CDName fileName[STRINGSIZE];
     // declares a file stream
     FILE* inFile;
     // print the message
     printf("Please Enter The File Name You Would Like To Open\n");
     // input users file
     scanf("%s", fileName);
     // echoprint user input
     printf("You Entered %s\n", fileName);
     // opens the file for read only
     inFile = fopen(fileName, "r");
     // test to make sure file opens 
     while (!fopen(fileName, "r"))
    	{
    	 // Prints Message
    	printf("File Opening Failed!\n");
    	// print the message
    	printf("Please Enter The File Name You Would Like To Open\n");
    	// input users file
    	scanf("%s", fileName);
    	// echoprint user input
    	printf("You Entered %s\n", fileName);
    	// opens the file for read only
    	inFile = fopen(fileName, "r");
    	} //while statement
     
     // prints the table
     printf("Your File Contents           #         Title\n"
    		"------------------------------------------------\n");
     // read in the title, author and then strip EOLNS 
     fgets ( info->artist, STRINGSIZE, inFile );
     fgets ( info->album, STRINGSIZE, inFile );
     info->artist [strlen (info->artist) - 1] = '\0';
     info->album [strlen (info->album) - 1] = '\0';
     
     fscanf(inFile, "%d", info->numTracks);
     
     printf("Artist: %s\n", info->artist);
     printf("Album: %s\n", info->album);
     printf("Number of tracks: %d\n", info->numTracks);
     
     while(info->numTracks > ARRAYSIZE)
    	{
        printf("Too Many Tracks!!\n");	
    	}
    
    // for ( lcv = 0; lcv < ARRAYSIZE; lcv++ )
    // {
    //
    // }
    
    }// Function ReadMusicData
    Now I am getting a debug error. It crashes when it goes to input with fgets. I am new to structs can someone tell me what I am doing wrong or point me to a good tutorial?

    Also if the number of tracks is bigger than 15 can I just call to the function again like this?

    Code:
    while(info->numTracks > ARRAYSIZE)
    	{
            printf("Too Many Tracks!!\n");
    	ReadMusicData(&info);
    	}
    Here is a sample data file:
    Code:
    Rolling Stones
    A Very Short CD to Begin Testing Your Program
    1
    Start Me Up
    3:32
    Last edited by jlharrison; 04-07-2006 at 08:32 AM.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    36
    This post needs a <bump>

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > jlharrison
    This user needs to read the forum rules
    http://cboard.cprogramming.com/annou...ouncementid=51

    And this moderator is closing the thread anyway.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-05-2008, 02:18 AM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Configurations give different results
    By Hubas in forum Windows Programming
    Replies: 2
    Last Post: 04-11-2003, 11:43 AM