[英]Why is my C program crashing the second time a function is called?
我是C语言的初学者,并负责构建具有各种功能的程序,这些程序可用于Yatzy游戏的实现。
这项工作仍在进行中,但是我被卡住了,当我运行它时一切都可以工作,直到我尝试选择先运行过throwDice()或readDieValues()之后再运行它们。 如果我选择多次调用printMenu()函数,它将起作用。 我猜问题出在for循环中,但我无法弄清楚哪里出了问题...帮助?
#include <stdio.h>
#include <stdlib.h> // rand(), srand()
#include <time.h> // time()
void printMenu(void);
void throwDice(int dice[], int nrOfDice, int nrOfDieValues);
void readDieValues(int dice[], int nrOfDice);
void printDice(const int dice[], int nrOfDice);
void printScores(const int dice[], int nrOfDice, int nrOfDieValues);
int isThreeOfAKind(const int dieValues[], int nrOfDieValues);
int main(void){
//Constants-Yatzy always has 5 die with no more and no less than values 1-6
const int nrOfDice = 5;
const int nrOfDieValues = 6;
int dice[nrOfDice]; //Holds the values for the die
int choice; //Holds the choice that the user inputs
//Prints the menu to the user at the beginning of the program
//Asks the user for a number between 1 and 4. (-1 terminates the program)
printMenu();
printf("\nMake your choice: ");
scanf(" %d", &choice );
//As long as the user doesn't want to terminate the program it
//excecutes the request and asks for a new number
while (choice!=-1){
switch(choice){
case 0:
printMenu();
break;
case 1:
throwDice(*dice, nrOfDice, nrOfDieValues);
break;
case 2:
readDieValues(*dice, nrOfDice);
break;
case 3:
break;
case 4:
break;
default:
printf("Invalid choice. \n");
}
printf("\nMake your choice: ");
scanf(" %d", &choice );
}
return 0;
}
/* Function: throwDice
* Description: Gives the 5 die random numbers between 1 and 6.
* Input: Optional to give a seed to the random-function.
* Output: None.
*/
void throwDice(int dice[], int nrOfDice, int nrOfDieValues){
int i;
int seed;
printf("Enter seed (1 gives a random seed): ");
scanf("%d", &seed);
// seed 1 sets the seed at "random", (uses the current time for seed)
//any other number will be used as a direct seed
if (seed==1){
srand(time(NULL));
}else{
srand(seed);
}
//Sets random values to each die
for (i=0; i<nrOfDice; i++){
dice[i]= (rand()%6)+1;
printf(" %d\n", dice[i]);
}
}
/* Function: readDieValues
* Description: Manually inputs values to 5 different die.
* Input: 5 positive integers between 1 and 6.
* Output: None.
*/
void readDieValues(int dice[], int nrOfDice){
int i;
//Sets values to each die from the user input
for(i=0; i<nrOfDice; i++){
printf("Die: ");
scanf(" %d", &dice[i]);
}
}
/* Function: printMenu
* Description: Prints out the menu to the user.
* Input: None.
* Output: A menu with different number choices.
*/
void printMenu(void){
printf("MENU: \n0. Display the menu \n1. Make a random throw \n"
"2. Enter die values for a throw \n3. Display the die values for the throw \n"
"4. Display the score for the throw \n-1. End program \n");
}
您正在使用这样的功能
case 1:
throwDice(*dice, nrOfDice, nrOfDieValues);
break;
case 2:
readDieValues(*dice, nrOfDice);
break;
但是dice
是一个数组int dice[nrOfDice];
。 在函数调用中放置*dice
就像放置dice[0]
。
case 1:
throwDice(dice, nrOfDice, nrOfDieValues);
break;
case 2:
readDieValues(dice, nrOfDice);
break;
您需要在dice
之前删除*
。
在switch
盒中用dice
替换*dice
,因为我们要传递骰子的地址而不是值
case 1:
throwDice(dice, nrOfDice, nrOfDieValues); //*dice
break;
case 2:
readDieValues(dice, nrOfDice); //*dice
int dice[]
是一个数组,因此, *dice
将具有数组的第一个元素,但是dice
将保留数组的(起始)地址。
在c语言中,我们仅将数组的地址传递给像void throwDice(int dice[])
这样的函数,以防止将数组的所有元素复制到堆栈中。
函数调用错误。 您应该写:
throwDice(dice, nrOfDice, nrOfDieValues);
和
readDieValues(dice, nrOfDice);
您没有收到编译器警告吗?
throwDice(* dice,nrOfDice,nrOfDieValues); 需要更改为
throwDice(dice,nrOfDice,nrOfDieValues);
一样
readDieValues(* dice,nrOfDice); 需要更改为
readDieValues(dice,nrOfDice);
当在开关中调用throwDice()和readDieValues()时,当这些函数需要一个int数组时,您传递* dice,它是dice数组中的第一个int。
要传递整个数组,只需使用其名称而不带任何星号:
throwDice(dice, nrOfDice, nrOfDieValues);
大多数人都指出了错误地调用函数的语法。 在我的回答中,我只是在澄清为什么您的呼叫错误的原因是通过骰子而不是* dice的原因是* dice引用了存储在dice指向的地址中的数据。 但是在原型中,您将第一个参数声明为指针,这迫使您传递“地址”以调用此函数。 数组也很容易,其名称用作指向内存起始地址的指针。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.