Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

C#
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:
C#
/*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:
C#
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

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
 
Share this answer
 
v2
Comments
Gilmore Sacco 2-Jun-13 14:26pm    
Thx Sergey Alenandrovich, but is its mandoratry for my assignment to use dynamic arrays :(
Sergey Alexandrovich Kryukov 2-Jun-13 20:58pm    
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.
Sergey Alexandrovich Kryukov 2-Jun-13 21:04pm    
OK, tell him... :-)
—SA
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.
 
Share this answer
 
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
CPallini 3-Jun-13 1:47am    
Thank you.
Sergey Alexandrovich Kryukov 2-Jun-13 20:56pm    
5ed.
—SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900