首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >分段故障:11个指针和结构

分段故障:11个指针和结构
EN

Stack Overflow用户
提问于 2016-02-28 17:25:00
回答 1查看 111关注 0票数 0

我在头文件中创建了两个结构,一个用于卡片,另一个用于处理,如下所示:

代码语言:javascript
复制
struct Card 
{                                 
    char *face; // define pointer face 
    char *suit; // define pointer suit
};


struct Hand
{
    struct Card *cards[15]; // define an array to hold a card
    char hand[13];  // defines the type of hand 
};

在我的printHand函数中,我尝试向每只手添加一张卡片,然后将这些手添加到一个代表所有玩家的数组中。我运行了一个调试器,在这里发生了分段错误:

代码语言:javascript
复制
// Here we will add the cards to the array holding the player's hands
totalHands[indexHand].cards[indexCard]->face = shuffledDeck[indexCard].face;
totalHands[indexHand].cards[indexCard]->suit = shuffledDeck[indexCard].suit;

调试器打印EXC_BAD_ACCESS。这是我的功能:

代码语言:javascript
复制
void printHand(struct Card* shuffledDeck, char* argv[])
{
    // total number of cards
    const int DeckMaxSize = 53;

    // get the command line input
    int numOfHands = atoi(argv[1]);
    int numOfCards = atoi(argv[2]);

    // counters for our loops
    int indexHand;
    int indexCard;

    struct Hand totalHands[10]; // an array holding all the hands

    // allocate memory for our struct
    struct Hand* tempHand = (struct Hand*)malloc(numOfCards * sizeof(struct Hand));

    // Adjusted the command line arguments if the number cards/hand and
    // players can not be resolved from a 52 sized deck
    int validateInput = DeckMaxSize / numOfHands;

    if (validateInput < numOfCards) {
        numOfCards = validateInput;
        printf("\n%s", "Input was adjusted to the size of the deck ( 52 )");
    }

    // Creates a new Hand and adds it to the totalHands[]
    for (indexHand = 1; indexHand <= numOfHands; indexHand++) {
        totalHands[indexHand] = *tempHand;
        printf("\n\n%s %d\n", "Player : ", indexHand);

        // add the cards to the hand and print each player's hand
        for (indexCard = 1; indexCard <= numOfCards; indexCard++) {
            // Here we will add the cards to the array holding the player's hands
            totalHands[indexHand].cards[indexCard]->face = shuffledDeck[indexCard].face;
            totalHands[indexHand].cards[indexCard]->suit = shuffledDeck[indexCard].suit;

            // Print all of our player's hand
            printf("\n%d %s ", indexCard, totalHands[indexHand].cards[indexCard]->face);
            printf("%s", totalHands[indexHand].cards[indexCard]->suit);
        }

        // print a new line for formatting
        printf("\n");
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-28 17:32:39

代码语言:javascript
复制
for(indexHand = 1; indexHand <= numOfHands; indexHand++){
...
for(indexCard = 1; indexCard <= numOfCards; indexCard++){       

这些循环从1n(最大索引包括)。但是索引应该从0n-1。因此,这些循环访问索引超出界限。

把你的循环修改成-

代码语言:javascript
复制
for(indexHand = 0; indexHand < numOfHands; indexHand++){
...
for(indexCard = 0; indexCard < numOfCards; indexCard++){       

编辑-

代码语言:javascript
复制
 totalHands[indexHand].cards[indexCard]->face = shuffledDeck[indexCard].face;
totalHands[indexHand].cards[indexCard]->suit = shuffledDeck[indexCard].suit;

在这两种语句之前,您不会为指针cards数组分配内存,但是您会取消引用它(未初始化的指针)。

分配此数组的内存指针,然后访问数组成员。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35685750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档