c - 带有在线编译器的 Mac 上的分段错误(核心转储)
问题描述
我对编程很陌生,我不明白为什么会出现分段错误,我什至不知道它是什么。如果你们能提供帮助,我将不胜感激。我正在使用 Mac 并使用在线编译器。我试过弄乱指针,我把它搞砸了,为什么会发生这种情况?
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
/*declare the function*/
void checkGuess(int luckyNum){
/*declare function vars*//**/
int p2prox, p1prox;
int p1guess,p2guess,p2score,p1score,*p1guessPtr,*p2guessPtr,*p1scorePtr,*p2scorePtr;
*p1scorePtr = 0;
*p2scorePtr = 0;
do{
do{
/*get the guess'*/
printf("Enter Player 1's Guess\n");
scanf("%d",&p1guess);
printf("Enter Player 2's Guess\n");
scanf("%d",&p2guess);
}while((p1guess && p2guess > 0) && printf("Wrong input try again"));
p1guessPtr = &p1guess;
p2guessPtr = &p2guess;
/*Find the proximities*/
p1prox = luckyNum - *p1guessPtr;
p2prox = luckyNum - *p2guessPtr;
/*Find, print and assign the scores*/
if(p1prox == p2prox){
*p1scorePtr++;
*p2scorePtr++;
printf("\nSame Guess! Both Recieved +1 Points!");
}
else if(p1prox > p2prox){
*p1scorePtr++;
printf("\nProximity of Player 1’s guess to the lucky number is:%d",p1prox);
printf("\nProximity of Player 2’s guess to the lucky number is:%d",p2prox);
printf("\nPlayer 1 get's a point!");
}
else{
*p2scorePtr++;
printf("Proximity of Player 1’s guess to the lucky number is:%d",p1prox);
printf("Proximity of Player 2’s guess to the lucky number is:%d",p2prox);
printf("Player 2 get's a point!");
}
}while(*p1guessPtr || *p2guessPtr != luckyNum);
printf("Proximity of Player 1’s guess to the lucky number is:%d",p1prox);
printf("Proximity of Player 2’s guess to the lucky number is:%d",p2prox);
if(p1prox == 0){
printf("Player 1 found the lucky number");
}
else{
printf("Player 2 found the lucky number");
}
}
int main(){
/*define the lucky number */
int luckyNum;
srand(time(0));
luckyNum=(rand() % 1000) + 1;
printf("A lucky number has been generated!");
printf("The lucky number is %d", luckyNum);
checkGuess(luckyNum);
return 0;
}
解决方案
推荐阅读
- android - 什么是 sdk_version:“module_current”与 sdk_version:“system_current”?
- java - Java 扫描器如何正确读取字符串
- android - 从图像选择器打开相机时,应用程序“失去与设备的连接”会颤抖。
- javascript - 如何获取地图中的每个元素并更改其 CSS?
- python - 为什么维特比算法(POS标注)总是预测一个标签?
- material-ui - 如何在无服务器堆栈中使用模块别名?
- javascript - 如何使用javascript自动计算2个日期和2个时间的持续时间?
- reactjs - 如何在数组中创建 React 元素(使用道具),然后通过简单地映射到该数组来渲染它们?
- c - 如何合并两个列表而不在下面的此链接上排序?
- ansible - 如何在分子中传递额外的库存文件(动态库存脚本)?