I am trying to access all values that I call. I'm not sure if I use array values correctly. In code below I'm trying to write values in file to memory and then call them by address. what is the problem? Am I overwriting on same memory adresses or I call them wrongly? Any help please?
my text file:
0 0 100 500 player1
0 1 400 450 player2
1 1 300 600 player3
FILE *fp;
struct ob {
int x;
int y;
int c_hp;
int max_hp;
char name[100];
int p_id;
};
struct ob *ptr;
int count = 0;
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: program <File name>\n");
exit(1);
}
read_player_values();
}
void read_player_values() {
ptr = (struct ob *)malloc(sizeof(struct ob));
if (ptr == NULL)
printf("out of memory");
else {
while (!feof(fp)) {
count++;
fscanf(fp, "%d%d%d%d%s", &ptr->x, &ptr->y, &ptr->c_hp, &ptr->max_hp,
ptr->name, &ptr->p_id);
ptr->p_id = count;
printf("%d %d %d %d %s %d", ptr->x, ptr->y, ptr->c_hp, ptr->max_hp,
ptr->name, ptr->p_id);
printf("\n");
}
fclose(fp);
printf("------\n");
for (int i = 0; i < count; i++) {
/* here is my problem */
/*trying to print all player names here*/
printf("%s\n", (ptr - i)->name);
}
}
}