我对 C 相当陌生,正在尝试制作骰子游戏。我正在尝试学习指针,需要一些关于使用在 main 中初始化的调用函数和指针的提示和建议。
我在第一次点名函数中遇到错误,以确定第一个滚轮:
被调用的对象“first_roll_call”不是函数或函数指针
这是什么意思,我该如何解决?
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main () {
int dice;
int roll, first_roll;
int i;
int first_roll_call;
int *playerA;
int *playerB;
int *first_rollA;
int *first_rollB;
playerA = (int *)malloc(100);
playerB = (int *)malloc(100);
first_rollA = (int *)malloc(100);
first_rollB = (int *)malloc(100);
printf("Welcome to Dice Mania!\n\n");
printf("First player to a combined score of 25 wins!\n");
printf("We to see who goes first, press 1 to continue:");
scanf("%d", &first_roll);
for (i = 1; i < 2; i++){
first_roll = rand() % 6 + 1;
first_roll_call(first_roll, i, &first_rollA, &first_rollB);
}
if (*first_rollA > *first_rollB){
printf("Player 1 goes first!\n");
}
else {
printf("Player 2 goes first!\n");
}
if (dice == 1) {
while (dice == 1) {
printf("Please press 1 to initiate the dice roll, 0 to exit\n\n");
srand(time(NULL));
roll = rand() % 6 + 1;
printf("You have rolled a: \n\n");
dice = dice_roll(roll);
if (dice == 0) {
printf("\n\nThank you for playing");
break;
}
}
}
return 0;
}
int dice_roll(int x) {
int dice;
if (x == 1) {
printf("*************\n");
printf("* ----- *\n");
printf("* | *\n");
printf("* | *\n");
printf("* | *\n");
printf("* | *\n");
printf("* ------- *\n");
printf("*************\n");
printf("Please press 1 to initiate the dice roll, 0 to exit\n\n");
scanf("%d", &dice);
return(dice);
}
if (x == 2) {
int dice;
printf("*************\n");
printf("* _____ *\n");
printf("* | *\n");
printf("* ____| *\n");
printf("* | *\n");
printf("* |____ *\n");
printf("* *\n");
printf("*************\n");
printf("Please press 1 to initiate the dice roll, 0 to exit\n\n");
scanf("%d", &dice);
return(dice);
}
if (x == 3) {
int dice;
printf("*************\n");
printf("* ----- *\n");
printf("* | *\n");
printf("* ___| *\n");
printf("* | *\n");
printf("* | *\n");
printf("* ----- *\n");
printf("*************\n");
printf("Please press 1 to initiate the dice roll, 0 to exit\n\n");
scanf("%d", &dice);
return(dice);
}
if (x == 4) {
int dice;
printf("*************\n");
printf("* | | *\n");
printf("* | | *\n");
printf("* |____| *\n");
printf("* | *\n");
printf("* | *\n");
printf("* | *\n");
printf("*************\n*");
printf("Please press 1 to initiate the dice roll, 0 to exit\n\n");
scanf("%d", &dice);
return(dice);
}
if (x == 5) {
int dice;
printf("*************\n");
printf("* _____ *\n");
printf("* | *\n");
printf("* |____ *\n");
printf("* | *\n");
printf("* _____| *\n");
printf("* *\n");
printf("*************\n");
printf("Please press 1 to initiate the dice roll, 0 to exit\n\n");
scanf("%d", &dice);
return(dice);
}
if (x == 6) {
int dice;
printf("*************\n");
printf("* _____ *\n");
printf("* | *\n");
printf("* |____ *\n");
printf("* | | *\n");
printf("* |____| *\n");
printf("* *\n");
printf("*************\n");
printf("Please press 1 to initiate the dice roll, 0 to exit\n\n");
scanf("%d", &dice);
return(dice);
}
int first_roll_call(int x, int y, int *first_rollA, int *first_rollB){
int dice;
if (x == 1) {
printf("*************\n");
printf("* ----- *\n");
printf("* | *\n");
printf("* | *\n");
printf("* | *\n");
printf("* | *\n");
printf("* ------- *\n");
printf("*************\n");
if (y = 1){
printf("Player one rolled a %d", x);
x = *first_rollA;
}
if (y = 2) {
printf("Player two rolled a %d", x);
x = *first_rollB;
}
}
if (x == 2) {
int dice;
printf("*************\n");
printf("* _____ *\n");
printf("* | *\n");
printf("* ____| *\n");
printf("* | *\n");
printf("* |____ *\n");
printf("* *\n");
printf("*************\n");
if (y = 1) {
printf("Player one rolled a %d", x);
x = *first_rollA;
}
if (y = 2) {
printf("Player two rolled a %d", x);
x = *first_rollB;
}
}
if (x == 3) {
int dice;
printf("*************\n");
printf("* ----- *\n");
printf("* | *\n");
printf("* ___| *\n");
printf("* | *\n");
printf("* | *\n");
printf("* ----- *\n");
printf("*************\n");
if (y = 1){
printf("Player one rolled a %d", x);
x = *first_rollA;
}
if (y = 2) {
printf("Player two rolled a %d", x);
x = *first_rollB;
}
}
if (x == 4) {
int dice;
printf("*************\n");
printf("* | | *\n");
printf("* | | *\n");
printf("* |____| *\n");
printf("* | *\n");
printf("* | *\n");
printf("* | *\n");
printf("*************\n*");
if (y = 1) {
printf("Player one rolled a %d", x);
x = *first_rollA;
}
if (y = 2) {
printf("Player two rolled a %d", x);
x = *first_rollB;
}
}
if (x == 5) {
int dice;
printf("*************\n");
printf("* _____ *\n");
printf("* | *\n");
printf("* |____ *\n");
printf("* | *\n");
printf("* _____| *\n");
printf("* *\n");
printf("*************\n");
if (y = 1) {
printf("Player one rolled a %d", x);
x = *first_rollA;
}
if (y = 2) {
printf("Player two rolled a %d", x);
x = *first_rollB;
}
}
if(x == 6) {
int dice;
printf("*************\n");
printf("* _____ *\n");
printf("* | *\n");
printf("* |____ *\n");
printf("* | | *\n");
printf("* |____| *\n");
printf("* *\n");
printf("*************\n");
if (y = 1) {
printf("Player one rolled a %d", x);
x = *first_rollA;
}
if (y = 2) {
printf("Player two rolled a %d", x);
x = *first_rollB;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
编译器在抱怨这一行main()
:
Run Code Online (Sandbox Code Playgroud)first_roll_call(first_roll, i, &first_rollA, &first_rollB);
它对first_roll_call
不是功能的抱怨是非常正确的。早些时候,main()
您将其声明为类型为 的局部变量int
:
Run Code Online (Sandbox Code Playgroud)int first_roll_call;
在内部main()
,该声明隐藏了同名的任何其他对象。如果您打算将其声明为返回 的函数int
,那么至少您需要这样做:
int first_roll_call();
Run Code Online (Sandbox Code Playgroud)
但是,实际上,您应该为该函数声明一个完整的原型:
int first_roll_call(int, int, int *, int *);
Run Code Online (Sandbox Code Playgroud)
此外,虽然允许在其他函数内部声明函数(但不能嵌套函数体!),但将函数声明放在文件范围内,靠近源文件的顶部是更传统的做法。
然而,对于这样一个相当简单的情况,将函数移动main()
到源文件的末尾将消除它声明其他函数的任何需要。编译器在到达 时已经看到附加到它们的函数体的声明main()
,这些就足够了。