简体   繁体   中英

Dynamic array of structs with dynamic array members in C

struct players playerList[1];
int main(){
    createPlayers(playerList);
    printf("%d", playerList[0].scores[2]);
}
struct players {
    char firstName[20];
    char lastName[20];
    char country[20];
    int scores[1];
    char *cards[1];
};

void createPlayers(struct players currentPlayers[]){
    int numPlayers;
    int numRounds;

    //Get input
    printf("How many players are there? ");
    scanf("%d", &numPlayers);
    printf("How many rounds are there? ");
    scanf("%d", &numRounds);

    //Allocate array of structures
    int* ptr1 = (int*)&currentPlayers;
    ptr1 = ( struct players * ) malloc ( sizeof ( struct players ) * numPlayers);

    //Allocate scores int array
    int* ptr2 = (int *)&currentPlayers[0].scores;
    ptr2 = malloc(numRounds * sizeof(int));

    //Allocate cards string array (array of pointers to char array)
    int* ptr3 = (int*)&currentPlayers[0].cards;
    ptr3 = (int *)malloc(sizeof(int) * numPlayers);
    for( int i = 0; i < numPlayers; i++ )
        currentPlayers[0].cards[i] = malloc( 3 * sizeof *currentPlayers[0].cards[i] );

    //Set scores[2] to 12
    currentPlayers[0].scores[2] = 12;
}

So I've had a lot of problems trying to get this to work. When I print playerList[0].scores[2] it prints 1886220131. I can print playerList[0].scores[0]/scores[1] just fine, but I can't seem to set anything past the second index. Do I need to allocate the original array of structs with the size of the dynamic arrays within the struct in mind? I'm new to C, but everything I try to do with the malloc seems to fail. Any help would be greatly appreciated! Thanks!

When you use playerList[1], scores[1], ...You are asking the computer to allocate an array consisting of 1 element. Then you pass the array to the function. Inside the function you assign ptr1 = currentPlayer and then you use malloc with ptr1 . This is meaningless. Think about it: x = 1 then x = 2 doesn't mean 1 = 2 . If you want to use malloc or realloc for your variable, don't use playerList[1], scores[1] or something, use the struct Players* playerList instead:

struct player* playerList;
playerList = (struct players*) malloc (sizeof(struct players) * numPlayers);

Next, if you want to change the value of the pointer in the function, you must pass the pointer of pointer into the function. That is, if you want to change the value of playerList in createPlayers function, you must pass &playerList . Then use:

*playerList = (struct players*) malloc (sizeof(struct players) * numPlayers);

Next, never cast the pointer's type to a new type that is completely different from the declared type. You declare a variable of type struct Player** but you cast it as type int* . This makes no sense, and also makes your program confusing.

Finally, limit the use of the global variable when it is not needed. Eg:

struct players playerList[1];

And when you use global variables, you don't need to pass it as a parameter.

enter image description here enter image description here I can't compile the code successfully, then I modify (struct players *) to (int *), it runs ok.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM