Thread: Can't add to my struct

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    4

    Can't add to my struct

    Hey I've got a minor problem in the code i'm writing, basically what i've done is to fill an character array with text i've read in from a text file.
    So the code for that is like this, tempstring is a line of text read in from the text file:
    Code to populate array (it's a 7x80 character array):
    Code:
    tempString = strtok( information, "\t" ); 
    
    int i = 0; //int for iterating through text array
    
    while (tempString != NULL ) {
    
    	printf("Cutting line \n");
    
    	strncpy(playerInformation[i], tempString, LINESIZE);				
    
    	//add to player struct
    
    	tempString = strtok( NULL, "\t" );
    
    	i = i + 1;
    }
    This is how i'm trying to fill my struct, the definition of this is below:
    Code:
    	typedef struct playerData *playersData;
    
    	struct playerData {
    
    		char *name;	//name of player
    
    		char *country;	//country of origin
    
    		char *innings;	//innings
    
    		char *runs;	//runs
    
    		char *nOut;	//not out
    
    		char *hScore;  //highscore
    
    		char *hsNOut  ;//if the high score is also not out
    
    		struct playerData *next; //pointer to the next player
    
    	};
    
    
    
    	//Create the struct to hold the players
    
    
    
    	typedef struct playerList pList;
    
    
    
    	struct playerList {
    
    		playersData *person;
    
    		pList *next;
    
    	};
    And here's my code to try and build up a player to insert
    Code:
    			playersData player = {playerInformation[0], playerInformation[1], 
    			playerInformation[2], playerInformation[3], playerInformation[4],
    			playerInformation[5], playerInformation[6]};
    Now the error i'm getting is:
    warning: excess elements in scalar initializer
    warning: (near initialization for ‘player’)

    I'm guessing that it's something to do with me not passing the right values to the array, but for the life of me I have no idea what to do to fix it. I know that a better way would be to build up the player in the loop i create the array in, but once again I don't know how to do that.
    I'm happy for any advice in either direction. Cheers
    Last edited by millsy; 10-24-2010 at 11:59 AM.

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    strncpy(playerInformation[i], tempString, LINESIZE);
    This is not right! What is playerInformation? Is it 2D array? There is very lack of information to really find the bug.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    Indeed it is a 2d array, 7x80.
    Pretty much my question boils down to, how do I copy the elements from my array into my struct.
    If I just type in
    Code:
    playersData player = {"string", "string", "string", "string", "string", "string", "string"};
    instead of
    Code:
    playersData player = {playerInformation[0], playerInformation[1], 
    			playerInformation[2], playerInformation[3], playerInformation[4],
    			playerInformation[5], playerInformation[6]};
    It'll work as expected, I'm thinking it's to do with the way that I'm referencing the array, maybe i'm not specifying a string?

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Yuo've typedef'd the struct playerdata as pointer

    Code:
    typedef struct playerData *playersData;
    And then you declare a varibale pointer of type *platerdata. And straight you start assigning without allocating space for fit. Should be

    Code:
    playersData player = malloc( sizeof( struct playerData ) * 1);
    /* do the error checking */
    player = {playerInformation[0], playerInformation[1], 
    			playerInformation[2], playerInformation[3], playerInformation[4],
    			playerInformation[5], playerInformation[6]};
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    Oh god it really was that simple, I can't believe how long i've been tearing out my hair over this... Thank you so much

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    Hey Harish I tried it again and for some reason it's giving me an error stating that it expects an expression before the '{' token on the line where I'm assigning values to the struct. Some googling appears to always point to not being able to assign values to an array dynamically.
    Same code as you posted
    Code:
    			playersData player = malloc( sizeof( struct playerData ) * 1);
    			//Fill this with information retrieved from the text file
    			player = {playerInformation[0], playerInformation[1], 
    			playerInformation[2], playerInformation[3], playerInformation[4],
    			playerInformation[5], playerInformation[6]};
    I've got an idea i'll try now and i'll check back with how it goes
    Last edited by millsy; 10-24-2010 at 02:51 PM.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    I dont really see any problems with the code which you have posted. But i cant really say where the issue is as i haven't seen the whole code. On the note try including some error checking mechanism. As left it becuase i was lazy. But always check the return value of malloc.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error in Exceptions.h file during build of QuickFix library
    By arupsarkar in forum C++ Programming
    Replies: 3
    Last Post: 07-16-2010, 10:30 AM
  2. mandelbrot set program improvements
    By mad_muppet in forum Game Programming
    Replies: 3
    Last Post: 07-14-2010, 05:58 AM
  3. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  4. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM