Thread: Having problems initialising a 2d array which is part of a struct

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    42

    Having problems initialising a 2d array which is part of a struct

    Hi all, I am having a little bit of difficulty initialising a 2d array, for a player of type struct.

    I've created a struct to represent a player, and created two players of this struct type, as so:

    Code:
    struct player
        { 
            int scoreC, scoreT, startScore, numDarts, legs, sets;
            int scoreCC[2][8];
            float dartAve;
            char name[15];
        };
    
        struct player player1, player2;
    I have created a 2d array to represent their score (the zeros will be updated as the game progresses):

    Code:
    player1.scoreCC[2][8] = {15,16,17,18,19,20,25,50,
                             0, 0, 0, 0, 0, 0, 0, 0};
    However the compiler just says "parse error before '{' token" which isn't giving me much to go on really. I have tried doing it for a 2d integer array that isn't part of a struct and it works fine:

    Code:
    int scoreCC[2][8] = {15,16,17,18,19,20,25,50,
                         0, 0, 0, 0, 0, 0, 0, 0};

    What am I doing wrong? I haven't been doing C for very long so I presume I'm making some silly error.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Even with a "free-standing" array, you can't initialize it's elements that way after it's been defined. You need to initialize it (as well as the other members) when the structure is instantiated, eg:

    Code:
    struct player player1 = 
    {
        0, // scoreT 
        0, // startScore
        0, // numDarts
        0, // legs
        0, // sets
        {
            15,
            16,
            17,
            18,
            19,
            20,
            25,
            50,
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0
        }, // scoreCC
        0.0, //dartAve
        "Joe" // name
    };

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    Quote Originally Posted by Sebastiani View Post
    Even with a "free-standing" array, you can't initialize it's elements that way after it's been defined. You need to initialize it (as well as the other members) when the structure is instantiated, eg:

    Code:
    <snip>
    I see.

    I have done this in other parts of the program before the 2d array was present (as shown below), and there was never a problem. Or do I now have to change the way in which i instantiate the variables because of the addition of the 2d array?

    Code:
    printf("\nHow many people are playing?: ");
        scanf("%d", &numPlayers);
        
        struct player player[numPlayers];
        
        for (i = 0; i < numPlayers; i++) 
        {
            printf("\nPlayer name: ", i);
            scanf("%s%c", &player[i].name, &x1);
            player[i].startScore = gameType;
            player[i].scoreC = player[i].startScore;
            player[i].numDarts = 0;
            player[i].scoreT = 0;
            player[i].dartAve = 0;
            player[i].sets = 0;
            player[i].legs = 0;
        }

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    No, it's perfectly fine (and usually more convenient) to perform the initialization later (often using a "set" function). And one more thing, the example I posted was not actually correct. Since the array is two-dimensional, the initialization would have been:

    Code:
    struct player player1 = 
    {
        0, // scoreT 
        0, // startScore
        0, // numDarts
        0, // legs
        0, // sets
        {
            {
    		15,
            	16,
            	17,
            	18,
            	19,
            	20,
            	25,
            	50
    	},	
            {
    		0, 
            	0, 
            	0, 
            	0, 
            	0, 
            	0, 
            	0, 
            	0
    	}	
        }, // scoreCC
        0.0, //dartAve
        "Joe" // name
    };

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    Ok thanks, so just for my understanding, why can I not initialise the 2d array in the same way as the other struct variables? As per your code example, I have to change the whole way in which I am initialising my player variables because of the introduction of a 2d array? Or am I not understanding it?

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by osiris^ View Post
    Ok thanks, so just for my understanding, why can I not initialise the 2d array in the same way as the other struct variables? As per your code example, I have to change the whole way in which I am initialising my player variables because of the introduction of a 2d array? Or am I not understanding it?
    For the same reason you can't do this:
    Code:
    int array[6];
    array[6] = {1,2,3,4,5,6}
    You can only populate an array that way when it is first declared. In this case it is already declared, when you instantiate a struct, as part of the struct. And you cannot populate an array inside the struct definition, either.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    Good stuff.

    Thanks for your help guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. array of struct pointers
    By simo_mon in forum C Programming
    Replies: 4
    Last Post: 05-11-2009, 08:34 PM
  3. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  4. how to pass 2D array into function..?
    By IngramGc in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 08:41 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM