我只是一个初学者,所以非常感谢您的帮助。在下面的代码中,我收到错误,似乎我做了一些错误的函数。程序输出如下所示,它假设随机生成一个如图所示的列表。
请对如何修复这个程序有什么建议。
#include <stdio.h>
void random_shuffle(deck, deck+52) {
}
/* initialize suit array */
const char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };
/* initialize face array */
const char *face[ 13 ] = { "Ace", "Deuce", "Three", "Four",
"Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen", "King" };
void for_each(int *start, int *end, void(*f)(int)) {
for (int *cur = start; cur < end; cur++) {
f(*cur);
}
}
void print_card(int n) {
printf("%s of %sn", face[n % 13], suit[n / 13]);
}
void shuffleAndDeal( int workdeck[][ 13 ], const char *workface[], const char *worksuit[] ) {
// srand((unsigned int)time(NULL));
int deck[52];
// Prime, shuffle, dump
for (int i=0;i<52;i++) {
deck[i] = i;
}
random_shuffle(deck, deck+52);
for_each(deck, deck+52, print_card);
return;
}
int main( int argc, char *argv[] ) {
/* initialize deck array */
int deck[ 4 ][ 13 ] = { 0 };
shuffleAndDeal( deck, face, suit );
return 0; /* indicates successful termination */
}
[![输出][1]][1]
下面是我得到的错误
void random_shuffle(deck, deck+52) {
^
main.c: In function ‘shuffleAndDeal’:
main.c:36:5: warning: implicit declaration of function ‘random_shuffle’ [-Wimplicit-function-declaration]
random_shuffle(deck, deck+52);
^~~~~~~~~~~~~~
main.c: At top level:
main.c:47:6: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘:’ token
https://www.onlinegdb.com/online_c_compiler#tab-stdin
^```
[1]: https://i.stack.imgur.com/bF7tl.png
这个…
void random_shuffle(deck, deck+52) { }
…不是一个有效的C函数定义。必须为每个函数参数表示类型和不同的标识符。您不需要为这两个参数指定类型,并且deck+52
不(仅仅)是一个标识符。