C 分段错误:11个指针和结构
我在头文件中创建了两个结构,一个用于卡片,一个用于手,如下所示:C 分段错误:11个指针和结构,c,pointers,memory,struct,C,Pointers,Memory,Struct,我在头文件中创建了两个结构,一个用于卡片,一个用于手,如下所示: 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
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
函数中,我试图向每一手牌添加一张牌,然后将这些手牌添加到代表所有玩家的数组中。我运行了调试器,分段错误发生在以下位置:
// 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\u BAD\u ACCESS
。以下是我的功能:
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");
}
}
void printHand(结构卡*shuffledDeck,字符*argv[])
{
//卡片总数
常数int DeckMaxSize=53;
//获取命令行输入
int numOfHands=atoi(argv[1]);
int numocards=atoi(argv[2]);
//循环计数器
int indexHand;
int索引卡;
struct Hand totalHands[10];//包含所有指针的数组
//为我们的结构分配内存
结构手*临时手=(结构手*)malloc(numocards*sizeof(结构手));
//如果数字卡/手和
//玩家不能从52码的牌堆中解题
int validateInput=DeckMaxSize/NumoHands;
如果(验证输入<数字卡){
numOfCards=验证输入;
printf(“\n%s”,“输入已调整为组(52)”的大小);
}
//创建新手牌并将其添加到totalHands[]
对于(indexHand=1;indexHand face=shuffledDeck[indexCard]。face;
totalHands[indexHand].cards[indexCard]>suit=shuffledDeck[indexCard].suit;
//打印所有玩家的手牌
printf(“\n%d%s”,indexCard,totalHands[indexHand].cards[indexCard]->face);
printf(“%s”,totalHands[indexHand].cards[indexCard]->suit);
}
//打印新行以进行格式化
printf(“\n”);
}
}
编辑-
在这两条语句之前,您不会将内存分配给指针数组卡
,但会取消对它的引用(未初始化的指针)
分配此数组的内存指针,然后访问数组成员。我也这么认为,但我仍然收到一个分段错误。.总指针的大小(10)比我传递的输入(4)大得多。我也制作了结构大小为15的卡片数组,但每个玩家最多13张。所以我真的不认为这是越界问题的索引。我相信这与指针或我访问它们的方式有关。@LesterRamos是的,可能是这样。我已经更新了答案。请看一看。P问题是你需要做的内存分配。啊,好吧,我想当我为手动结构分配内存时,它会得到处理。所以从语法上讲,我如何访问tempHand结构中的卡数组并为其分配内存?使用
tempHand
访问循环和使用malloc
将内存分配给它。我在嵌套for循环中这样做,但仍然收到该死的分段错误。tempHand->cards[indexCard]=(struct Card*)malloc(numocards*sizeof(struct Card));
for(indexHand = 1; indexHand <= numOfHands; indexHand++){
...
for(indexCard = 1; indexCard <= numOfCards; indexCard++){
for(indexHand = 0; indexHand < numOfHands; indexHand++){
...
for(indexCard = 0; indexCard < numOfCards; indexCard++){
totalHands[indexHand].cards[indexCard]->face = shuffledDeck[indexCard].face;
totalHands[indexHand].cards[indexCard]->suit = shuffledDeck[indexCard].suit;