Hi i am writing some program where declare 3 stuctures code below
Code:
typedef struct Card
{
char color;
int face;
}CardT;
typedef struct Player
{
char side;
Card *card;
}PlayerT;
typedef struct Deal
{
Player *Gracze;
int PointsNS;
int PointsEW;
}DealT;
when i am trying to write something in stuctures i got an error type read acces violation
Code:
void WczytajRozdanie(DealT *Rozdanie)
{
char temp[3] = { '\0' };
char linia[LINE_SIZE] = { '\0' };
FILE *FileT = fopen("C:\\Users\\P\\Desktop\\rozdanie.txt", "r");
if (Rozdanie == NULL)
{
printf("Nie mozna otworzyc pliku\n");
return;
}
Rozdanie->Gracze = (PlayerT*)malloc(sizeof(PlayerT) * PLAYERS);
while (fgets(linia, LINE_SIZE, FileT) != NULL)
{
for(int i=0;i<PLAYERS;i++)
{
Rozdanie->Gracze[i].card = (CardT*)malloc(sizeof(CardT) * FACE);
Rozdanie->Gracze[i].side = linia[0]; // this works well
for (int j = 0, len =2 ; j < FACE; j++, len += 3)
{
sprintf(temp, "%2s", linia + len);
Rozdanie->Gracze[i].card[j].color = temp[1];//there is an error
if ((temp[0] >= '0')&&(temp[0] <= '9'))
Rozdanie->Gracze[i].card[j].face = temp[0] - '0';
else if (temp[0] == 'T')
Rozdanie->Gracze[i].card[j].face = 10;
else if (temp[0] == 'J')
Rozdanie->Gracze[i].card[j].face = 11;
else if (temp[0] == 'Q')
Rozdanie->Gracze[i].card[j].face = 12;
else if (temp[0] == 'K')
Rozdanie->Gracze[i].card[j].face = 13;
else if (temp[0] == 'A')
Rozdanie->Gracze[i].card[j].face = 14;
}
}
printf("%s", linia);
printf("\n\n");
}
}
Code:
Rozdanie->Gracze[i].card[j].color = temp[1];
there is an error
Can you tell me why this happend and how to fix it