我正在编写的二十一点程序中的交易功能遇到问题。谁能看到发生了什么?当我尝试在 main() 中打印时,出现分段错误。
干杯。
主功能
int main(void)
{
int i, j, k, purse;
card deck[52];
card *head_d, *temp_d, *current_d = NULL, *last_d, *head_p, *temp_p, *current_p = NULL, *last_p;
make(deck); //CREATES THE DECK
shuffle(deck); //SHUFFLES THE DECK
deal(deck, head_d, current_d, head_p, current_p)
for(i = 0; i < DECK_SIZE; i++)
{
printf("%d: %d %c\n", i + 1, deck[i].face, deck[i].suit);
}
temp_p = head_p;
while(temp_p != NULL)
{
printf("%d %c\n", temp_p->face, temp_p->suit);
temp_p = temp_p->listp;
}
return(0);
}
函数交易()
void deal(card x[DECK_SIZE], card *head_d, card *current_d, card *head_p, card *current_p)
{
int i;
card *temp_p, *temp_d;
for(i = 0; i < 4; i++)
{
if( i % 2 == 0)
{
temp_p = (card *)malloc(sizeof(card));
temp_p->face = x[i].face;
temp_p->suit = x[i].suit;
if (current_p==NULL)
{
head_p=temp_p;
}
else
{
current_p->listp=temp_p;
}
current_p = temp_p;
temp_p->listp = NULL;
}
else
{
temp_d=(card *)malloc(sizeof(card));
temp_d->face = x[i].face;
temp_d->suit = x[i].suit;
if (current_d==NULL)
{
head_d=temp_d;
}
else
{
current_d->listp=temp_d;
}
current_d = temp_d;
temp_d->listp = NULL;
}
}
}