Hi, I'm having troubles with the following code:
Code:
#include "jugador.h"
struct JugadorStruct_t {
int fichas, manos_ganadas, manos_perdidas, manos_empatadas;
char* nombre;
int* fichas_partidas;
};
int initJugador(JugadorPtr_t jugador) {
//PRE: si jugador.txt existe, tiene al menos 4 filas
//POST: si 1, el puntero apunta a un jugador inicializado con el fichero.
//si 0, error abriendo el archivo
FILE* fp = fopen("/home/norhther/CLionProjects/blackjack/jugador.txt","r");
if (fp == NULL) {
printf("El archivo del jugador no existe\n");
fclose(fp);
return 1;
}
else {
char* line = NULL;
size_t len = 0;
getline(&line, &len, fp);
jugador->nombre = strdup(line);
getline(&line, &len, fp);
jugador->fichas = atoi(line);
getline(&line, &len, fp);
jugador->manos_ganadas = atoi(line);
getline(&line, &len, fp);
jugador->manos_perdidas = atoi(line);
getline(&line, &len, fp);
jugador->manos_empatadas = atoi(line);
int manos = jugador->manos_perdidas + jugador->manos_ganadas + jugador->manos_empatadas;
int *p = ((int*)malloc(manos*sizeof(int)));
jugador->fichas_partidas = p;
int i;
for (i = 0; i < manos; i++) {
getline(&line, &len, fp);
p[i] = atoi(line);
}
}
fclose(fp);
return 0;
}
jugador->fichas_partidas = p is giving me this error, and I don't know why
it is happening. Any clues?
Thanks!
Edit: I found the following:
in my jugador.h file I've got the following definition
Code:
typedef struct JugadorStruct_t * JugadorPtr_t;
and I allocate the space for this in my main like this:
[CODE]
JugadorPtr_t jugador = malloc(sizeof jugador);
[/CODE]
I tried JugadorPtr_t jugador = malloc(sizeof jugador * 50) and now I don't get the error. Any clue?