首页 > 解决方案 > 带有在线编译器的 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;
}



标签: cmacossegmentation-faultcoredump

解决方案


推荐阅读