2017-03-19 9 views
1

私はそれが私のポインタ変数と関係していることを知っています。私の目標は、私が与えられた既存のコードにスーツの色を実装することです。そこで、デッキ構造の下にcharポインタを作成しました。私がシャッフルするまで、すべてのものが正しく印刷されます。その場合、セグメンテーション違反が発生します。Cでカードをシャッフルするときにセグメンテーションフォルトが発生する理由をよくわかりません

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

、あなたがdeckをシャッフルするときチャンスはあなたが別のカードを取得したいということである:あなたのカードは、あなたが実際にここを除いて、使用しないcolorを持っているので、これは

#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"); 
     } 

    } 


} 
+0

を印刷するために、ランクとスーツを表すために列挙型を使用したい、それはですC – SurvivalMachine

+0

'(赤)'の代わりにヌルポインタが表示されます –

+0

@AnttiHaapalaさて、私はこのサイトが初めてです。 – thale

答えて

2

その.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) "); 
    } 

にeが、私は個人的に例

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

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

用とC#ではありません

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

... 
printf("%s", suit_to_string[deck[i].suit]); 
関連する問題