Reputation: 81
typedef struct Carta* BAR_tppCarta
BAR_tppCarta * BAR_CriarBaralho ()
{
int i;
int j;
int k=0;
BAR_tppCarta *baralho;
baralho = (BAR_tppCarta *) malloc(NUM_CARTAS*sizeof(BAR_tppCarta));
if(!baralho)
return NULL;
for(i=COPAS;i<=ESPADA;i++)
for(j=AS;j<=KING;j++)
{
baralho[k]->naipe = i;
baralho[k]->valor = j;
k++;
}
return baralho;
}
when I call this function in another module, using
BAR_tppCarta *baralho = BAR_CriarBaralho();
The windows stop working, like a debug error. Can anyone help me please?
Very thanks! Alessandro
Upvotes: 0
Views: 103
Reputation: 213170
BAR_tppCarta
is just a pointer, so all you are returning is an array of uninitialised pointers (and you are also stomping over memory). I suspect that this line:
typedef struct Carta* BAR_tppCarta;
should probably be:
typedef struct Carta BAR_tppCarta;
and these two lines:
baralho[k]->naipe = i;
baralho[k]->valor = j;
should be:
baralho[k].naipe = i;
baralho[k].valor = j;
Alternatively you can keep the original definition of BAR_tppCarta
as
typedef struct Carta* BAR_tppCarta;
and then allocate memory for each instance:
for(i=COPAS;i<=ESPADA;i++)
for(j=AS;j<=KING;j++)
{
baralho[k] = malloc(sizeof(struct Carta));
baralho[k]->naipe = i;
baralho[k]->valor = j;
k++;
}
although I would advise against this on the grounds of complexity unless you have a good reason to do it this way.
Upvotes: 7