gpt4 book ai didi

c - C程序中的指针问题

转载 作者:行者123 更新时间:2023-11-30 19:43:19 24 4
gpt4 key购买 nike

好吧,我正在开发一个处理一手牌的项目,需要能够知道每种花色有多少张以及每张牌面有多少张,以便可以对其进行评估。 (我不希望你为我做这件事,我只是需要一些帮助理解。)我不断收到这样的警告:

“格式参数太多”

“从不兼容的指针传递 'suitsInHand 的参数 1”

“初始化从指针生成整数而不进行转换”

“指针与整数的比较”

我已经将关于指针的章节重读了三遍,但我仍然对如何使用它们感到困惑。如果有人可以解释我的代码有什么问题,以便我可以尝试正确编码,我将非常感激。我在警告行周围加上了 ** **,以便我编写它们。

这些是在程序开头定义的函数

// prototypes
void shuffle( unsigned int wDeck[][ FACES ] ); // shuffling modifies wDeck
void deal( unsigned int wDeck[][ FACES ], const char *wFace[], const char *wSuit[] ); // dealing doesn't modify the arrays
void handDeal( unsigned int wDeck[][ FACES ], const char *wFace[], const char *wSuit[] );
void determineHand( unsigned int suitsInHand[], unsigned int facesInHand[] );

int suitsInHand( unsigned int wDeck[][ FACES ], const char *wSuit[ ] );
int facesInHand( unsigned int wDeck[][ FACES ], const char *wFace );

这是我遇到最多麻烦的函数的稍后部分。 wDeck 和 wFace 来自洗牌和发牌函数。这手牌发了 5 张牌,问题在于如何获取这些信息。

void handDeal( unsigned int wDeck[][ FACES ], const char *wFace[], const char *wSuit[] )
{
size_t cardCount;
size_t row;
size_t column;

// deal each of the cards
for ( cardCount = 1; cardCount <= 5; ++cardCount) {
// loop through the rows of wDeck
for ( row = 0; row < SUITS; ++row ) {
// loop through columns of deck for current row
for ( column = 0; column < FACES; ++column ) {
// if slot contains current card, display card
if ( wDeck[ row ][ column ] == cardCount ) {
**printf("\n%5s of %-8s", wFace[ column ], wSuit[ row ], cardCount);**
} // end if
} // end 2nd inner for
} // end inner for
} // end outer for

//int *suitPtr = &wSuit[ & wFace[ column ], &wSuit[ row], cardCount ];
//in *facePtr = &wFace[ column ];

**suitsInHand( &wDeck, wSuit[ row ] );**
//facesInHand( &wDeck, &wFace[ column ] );

} // end function handDeal

// determine suits in hand
int suitsInHand( const char )
{
size_t suitCount; // counter
size_t row;
int totalSuits; // total number of suits in hand
**int suit = wSuit[ row ];**

// determine number of suits
for ( suitCount = 0; suitCount <= 4; ++suitCount ) {
**if ( wSuit[ row ] <= 4 ) {**
totalSuits = suit % 4;
printf( "\nYou have %d suits", totalSuits );
} // end if
} // end for

} // end function suitsInHand

// determine faces in hand
/*int facesInHand( unsigned int wDeck[][ FACES ], const char *wFace[] )
{
int totalFaces = 0;

*facePtr = *facePtr % 13;

printf( "\nYou have %d faces", *facePtr );

} // end function facesInHand */

最佳答案

在声明中 void handDeal( unsigned int wDeck[][ FACES ], const char *wFace[], const char *wSuit[] ) 您将 wFace 和 wSuit 指定为指向数组的指针。它相当于声明 char** wSuit 的指针。

当您分配一个数组时,例如 char wSuit[5],wSuit 指向第一个数组成员。您可以通过索引 wSuit[0] 或通过取消引用指针 *wSuit 来获取第一个数组成员的值。当您指定指向该数组的指针(即 char* wSuit[] 或 char** wSuit)时,您将创建一个指针,该指针保存指向数组第一个成员的指针的地址。这有道理吗?

这里遇到的另一个问题是您使用 undefined variable 在堆栈上分配数组。在编译时,row 的值是未知的,因此编译器无法正确分配数组所需的内存。使用定义的值(例如 5)或动态分配内存(例如 (char*)malloc(sizeof(char)*row))应该适合您。

我在这里看到了一些其他问题,但希望这能让您更进一步。

关于c - C程序中的指针问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29758542/

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