好的,我正在做一个处理一纸牌的项目,并且需要能够知道每套衣服有多少张,每张面孔有多少张,以便可以对其进行评估。 (我不希望您为我这样做,我只需要一些帮助即可理解。)我不断收到警告,指出:
“格式的参数过多”
“从不兼容的指针传递'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)时,您将创建一个指针,该指针保存指向该数组第一个成员的指针的地址。那有意义吗?
这里的另一个问题是您正在使用一个未定义的变量在堆栈上分配数组。在编译时,row
的值是未知的,因此编译器无法正确分配数组所需的内存。使用定义的值(例如5)或动态分配内存(例如(char *)malloc(sizeof(char)* row))应该适合您。
我在这里看到了其他一些问题,但是希望这可以使您更进一步。
关于c - C程序中的指针问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29758542/