gpt4 book ai didi

c - 不确定为什么在 C 中洗牌时出现段错误

转载 作者:太空宇宙 更新时间:2023-11-04 02:31:22 24 4
gpt4 key购买 nike

我知道这与我的指针变量有关,我的目标是将西装的颜色应用到我之前获得的代码中。所以我在甲板结构下创建了一个字符指针。它会正确打印出所有内容,直到我开始随机播放,在这种情况下我会遇到段错误。

 #include <stdio.h>
#include <time.h> //time function
#include <stdlib.h> //random number generator functions
#include <string.h> //need for Linux

#define MAX 9
#define MAX_CARDS 52
#define MAX_RANKS 13
#define MAX_SUITS 4
#define COLS 2 //number of columns to display in output

//structure definition
struct card{
char *rank;
char suit[MAX];
char *color;
};
typedef struct card Card;

//array of pointers to strings for ranks
char *ranks[MAX_RANKS] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack", "Queen", "King"};

char *color[2] = {"(black)", "(red)"};

//two-dimensional array of strings for suits
char suits[MAX_SUITS][MAX] = {"Clubs", "Diamonds", "Hearts", "Spades"};
//[0][1] [0][2][0][3]

void initialize(Card []);
void shuffle(Card []);
void display(const Card[]);

int main(){
char newline = '\n'; //to repeat while loop
//declare an array of 52 cards

Card deck[MAX_CARDS] = {"","",""};
initialize(deck);
deck[0].color = "(black)" ;
deck[1].color = "(red)";
printf("Display an ordered deck of cards:\n");
display(deck);
while('\n' == newline){
printf("\nshuffling deck ... \n\n");
shuffle(deck);
display(deck);
printf("\n\nWould you like to shuffle again?\nIf so, press \"Enter\" key. If not, enter any other char. ");
newline = getchar();
}
return 0;
}

/*
initialize the deck of cards to string values
deck: an array of structure cards
*/
void initialize(Card deck[]){
int i = 0;
for(i=0;i<MAX_CARDS;i++){
deck[i].rank = ranks[i%MAX_RANKS];
strncpy(deck[i].suit, suits[i/MAX_RANKS], MAX);
}
}

/*
use the pseudo-random number generator to shuffle the cards
deck: an array of structure cards
*/
void shuffle(Card deck[]){
int swapper = 0; //index of card to be swapped
int i = 0; //counter
Card temp = {"", ""}; //temp holding place for swap
srand(time(NULL)); //seed the random numbers with current time
for(i=0;i<MAX_CARDS;i++){
//generate a pseudo-random number from 0 to 51
swapper = rand() % MAX_CARDS;
//swap current card with da swapper
temp = deck[i];
deck[i] = deck[swapper];
deck[swapper] = temp;
}
}

/*
display the deck of cards
deck: an array of structure cards
*/
void display(const Card deck[]){
int i = 0;
for(i=0;i<MAX_CARDS;i++)
{
printf("%5s of %-10s", deck[i].rank, deck[i].suit);//%5s makes "of" line up -10 makes cols.
if (strcmp(deck[i].suit, "Clubs") == 0 || strcmp(deck[i].suit, "Spades") == 0)
{
printf(" %5s", deck[0].color);
}
else
{
printf(" %5s", deck[1].color);
}

//put in a newline every %x loops
if((COLS-1) == (i%COLS)){
printf("\n");
}

}


}

最佳答案

这是因为你的卡片有你实际上不使用的颜色,除了这里:

deck[0].color = "(black)" ;
deck[1].color = "(red)";

现在,当您洗牌 deck 时,您可能会在那里得到另一张牌,其 .color 仍然是 NULL,并且您的代码在 中崩溃printf("...%s...", deck[0].color);

解决方案当然是 a) 完全删除 color 成员,b) 然后将打印代码修改为

    if (strcmp(deck[i].suit, "Clubs") == 0 || strcmp(deck[i].suit, "Spades") == 0) {
printf(" (black)");
}
else {
printf(" (red) ");
}

但是,我个人会使用枚举来表示等级和花色,例如

enum Suit {
HEARTS = 0,
SPADES,
DIAMONDS,
CLUBS
};

struct Card {
enum Suit suit;
...
};

打印

char *suit_to_string[4] = {"Hearts", "Spades", "Diamonds", "Clubs"};

...
printf("%s", suit_to_string[deck[i].suit]);

关于c - 不确定为什么在 C 中洗牌时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42884380/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com