Click here to Skip to main content
14,641,771 members
Rate this:
Please Sign up or sign in to vote.
See more:
hello experts,

In my previos post, alot of help was givin, but still i cant figure this out. I have a function as described here under, and the problem with this code is found in the for loop as indicated by the compiler.

void ReadHighScores(FILE *Scores,int *elements, player_t *playersData)
{
     Scores = fopen("Scores.txt","r");
     int temp,i;
     player_t *playersTemp;

     /*getting size of array*/
     fscanf(Scores, "%d\n", elements);

     playersTemp = (player_t*)realloc(playersData,(*elements) * sizeof(player_t));
     playersData = playersTemp;
     free(playersTemp);

     for(i=0; i<(*elements); i++ )
     {
         /*Getting Name*/
         fscanf(Scores, "%s", playersData[i].Name);
         /*Getting Score*/
         fscanf(Scores, "%d", &playersData[i].Score);
     }

     fclose(Scores);

}


I am calling this function from the main as shown in the code below:
/*Getting the data from file*/
player_t *players;
players = (player_t*)malloc(1 * sizeof(player_t));

ReadHighScores(&Scores,&elements,&players);
for(i=0; i<(*elements); i++ )
{
         printf("%s\n",players[i].Name);
         printf("%d\n",players[i].Score);
}


The struct that am using:
typedef struct
{
     char Name[name_l];
     short Score;
}player_t;



All help will be greatly appreciated. Thanks Experts
Posted
Updated 2-Jun-13 7:22am
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

If you really want to change the size of arrays during runtime, array types are not quite suitable, due to the need for reallocation, which is inefficient. You should better think of implementing of resizeable container, such as linked list: http://en.wikipedia.org/wiki/Linked_list[^].

Also, pay attention for the section "Related structures". Linked list is just one of the simplest structures.

—SA
   
v2
Comments
Gilmore Sacco 2-Jun-13 14:26pm
   
Thx Sergey Alenandrovich, but is its mandoratry for my assignment to use dynamic arrays :(
   
Whatever, use the solution of CPallini. However, if this is because it's needed for an assignment, is should be a study assignment, and as such, it should be something you do by yourself. You should learn writing and debugging code, to make things done.
—SA
Gilmore Sacco 2-Jun-13 21:01pm
   
Cpallini did not modify the code, the only explained what was happening in my opinion.
   
OK, tell him... :-)
—SA
Rate this:
Please Sign up or sign in to vote.

Solution 2

Quote:
/*getting size of array*/
fscanf(Scores, "%d\n", elements);

playersTemp = (player_t*)realloc(playersData,(*elements) * sizeof(player_t));
playersData = playersTemp;
free(playersTemp);

  • You should verify that elements is valid for your application (e.g. suppose the user inserts a negative value).
  • You should always check the return value of realloc.
  • you MUST NOT call free(playersTemp). The call in your code is a blunder, because then you are trying to assign values to such released memory.


Please note playersData = playersTemp; has the effect of making both playersData and playersTemp variable to point to the same memory area, namely the one freshly reallocated. If you free such memory then both the pointers would point to released (that is invalid) memory.
   
Comments
Gilmore Sacco 2-Jun-13 16:32pm
   
Yeahh now i understand, the lecturer mentioned that! Thanks sir, Very helpfull
CPallini 2-Jun-13 16:42pm
   
You are welcome.
Espen Harlinn 2-Jun-13 20:29pm
   
5'ed :-D
   
5ed.
—SA
CPallini 3-Jun-13 1:47am
   
Thank you.
CPallini 3-Jun-13 1:47am
   
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100