0

I have a Team struct which has field of pointer to a struct called Player. I have a function that returns a pointer to a player, everytime the user adds a player, it suppose to re-allocate the memory size of the "array" that contains the players, and to add a new player. like a dynamic array of players. for some reason it only accepts one player. I have seen problems that resembles mine, but no answer worked. Please help me.

the code:

struct Player {
float pointsAverage;
int uniformNumber;
int pointsOfLastFiveGames[5];
char *playerName;
};

struct Team {
    int numberOfPlayers;
    char *managerName = nullptr;
    struct Player *players = (Player*)malloc(sizeof(Player));
};

struct Player* setPlayer(){
    struct Player *player = NULL;
    player = (struct Player*)malloc(sizeof(struct Player));
    char name[81] = {0};
    int jerseyNumber = -1;
    int sumOfPoints = 0;
    int currentPoint = 0;
    printf("Player name?\n");
    scanf("%s", &name);
    int nameLength = strlen(name);
    player->playerName = (char*)malloc(sizeof(char)*(nameLength + 1));
    strcpy(player->playerName, name);
    printf("Jesrsey number?\n");
    scanf("%d",&jerseyNumber);
    player->uniformNumber = jerseyNumber;
    printf("Player point history?\n");
    int i;
    for (i=0; i<NUM_OF_PLAYER_POINT; i++) {
        scanf("%d",&currentPoint);
        sumOfPoints+=currentPoint;
    }
    player->pointsAverage = (float)(sumOfPoints/NUM_OF_PLAYER_POINT);
    return player;
}

struct Team* setTeam(){
    //initialize a team
    struct Team *team = NULL;
    team = (struct Team*) malloc(sizeof(struct Team));
    team->managerName = NULL;
    team->numberOfPlayers = 0;
    team->players = NULL;
    int usersChoice = -1;
    // temp char array of name so i can take its size after user input
    char name[81] = {0};
    printf("Hello, please enter manager's name:\n");
    scanf("%s",&name);
    // checking the length of the user input.
    int nameLength = strlen(name);
    // dynamically allocating the manangerName memory space.
    team->managerName = (char *)malloc(sizeof(char) * (nameLength + 1));
    // setting the managername to the name the user typed.
    strcpy(team->managerName, name);
    int playerCounter = 1;
    int addedPlayerCounter = 0;
    while (1){
        printf("Would you like to buy a player (1) yes (else) no?\n");
        scanf("%d",&usersChoice);
        if(usersChoice==1){
            usersChoice = 0;
            playerCounter++;

            Player *temp = NULL; // here I tried make a new pointer but it still doesn't work
            struct Player* tempPlayer = setPlayer();
            temp = (Player*)realloc(team->players, sizeof(Player)*playerCounter);
            (temp+(sizeof(Player)*addedPlayerCounter))->playerName = tempPlayer->playerName;
            (temp+(sizeof(Player)*addedPlayerCounter))->pointsAverage = tempPlayer->pointsAverage;
            addedPlayerCounter++;
            team->numberOfPlayers++;

        }else{
            return team;
        }
    }
}

the main function just calls the setTeam function.

  • 1
    You never assign `temp` to any variable. It is lost after you leave that `if` block – Gerhardh Feb 17 '23 at 08:28
  • In C you [should not cast the result of `malloc` or `calloc` or `realloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858). Ad using the pointer-to operator `&` in e.g. `scanf("%s", &name)` is wrong (use plain `name` in that case). – Some programmer dude Feb 17 '23 at 08:28
  • `scanf("%s",&name);` You should always check return value of `scanf` and you should add a length limit to all your string input. Also `name` already decays to a pointer. Don't add `&` there: `if (scanf("%80s",name) != 0) { error handling }` – Gerhardh Feb 17 '23 at 08:29
  • 1
    @NoobProgrammer This is not a C code. – Vlad from Moscow Feb 17 '23 at 08:30
  • 1
    `(Player*)malloc(` There is no type `Player` defined in the code you show. – Gerhardh Feb 17 '23 at 08:31
  • the `&` in `scanf("%s",&name)` is the coding style the University told me to use. @Gerhardh the type Player is a struct, it the first thing in the code I posted. – NoobProgrammer Feb 17 '23 at 08:34
  • 1
    What is this mess supposed to do? `(temp+(sizeof(Player)*addedPlayerCounter))->playerName`. You need to revisit your learning material about pointer arithmetics. This is same as `temp[sizeof(Player)*addedPlayerCounter].playerName`. The `sizeof` part is clearly wrong. Try `temp[playerCounter].playerName` – Gerhardh Feb 17 '23 at 08:34
  • You need a `&` for `scanf` if the provided is not already a pointer. You need it for integers, characters etc. but not if you pass an array which decays to a pointer – Gerhardh Feb 17 '23 at 08:35
  • @Gerhardh this mess it the thing that don't work probably, I'm trying to get to the address of the second element in the "array". – NoobProgrammer Feb 17 '23 at 08:36
  • 1
    No, the first type in your code is `sruct Player`, not `Player`. In C++ you could it use as `Player`. In C you must use `struct Player` unless you provide a typedef. Do you use C or C++? They are very different languages – Gerhardh Feb 17 '23 at 08:37
  • the Uni wanted the file to be C++ but the code itself is C. I don't why they wanted that. but it works the answer with the sizeof is right. God bless you @Gerhardh, can you please explain to me why the `sizeof` part is wrong? I though that because it is not an array if `team-players` leads me to address 600, and `Player` size is 8, then from 600 to lets say 608 is the first player, and 609-617 is the second and so on. this is why I used `sizeof` to get to the second `Player`. – NoobProgrammer Feb 17 '23 at 08:39
  • That still leaves the error with not saving `temp` which will cause your code fail after first iteration of the loop. If you are supposed to write C code, then write C code. You rely on it being compiled as C++ code. – Gerhardh Feb 17 '23 at 08:42
  • @Gerhardh, can you please explain to me why the `sizeof` part is wrong? I though that because it is not an array if `team->players` leads me to address 600, and Player size is 8, then from 600 to lets say 608 is the first player, and 609-617 is the second and so on. this is why I used `sizeof` to get to the second Player. – NoobProgrammer Feb 17 '23 at 08:43
  • The array already takes care about the size of each element. an `int a[4]` can be accessed via index `0..3`, not `0..3*sizeof(int)`. You need to revisit basic learning materials about array and pointer arithmetics. – Gerhardh Feb 17 '23 at 08:45
  • Are you sure that you are use C and not C++? `char *managerName = nullptr; struct Player *players = (Player*)malloc(sizeof(Player));` in the `struct Team` definition is only valid syntax in C++. – mch Feb 17 '23 at 08:46
  • @mch the file is C++, I guess I wrote C++ then. – NoobProgrammer Feb 17 '23 at 08:48

0 Answers0