c - Why does this code yield a segmentation fault? -
i writing program goal generate random 3 digit number, , have user make 10 guesses @ it. if guess correct digit in correct spot, considered "hit". if guess correct digit it's in wrong spot, considered "match". example, if number guessed 123, , enter 329, 2 hit , 3 match. code far shown below:
#include <stdio.h> #include <time.h> #define min 100 #define max 999 int main() { //declare variables int userdig1 = 0, userdig2 = 0, userdig3 = 0, randdig1 = 0, randdig2 = 0, randdig3 = 0, guesses = 0; //generate random 3 digit number srand(time(null)); //seed randdig1 = rand() % ((max + 1) - min) + min; //corresponds first digit randdig2 = rand() % ((max + 1) - min) + min; //corresponds second digit randdig3 = rand() % ((max+ 1 ) - min) + min; //corresponds third digit //a loop keeps track of number of guesses (guesses = 1 ; guesses <= 10 ; guesses++) { //store user's guess appropriate variables printf("enter guess number %d:\n", guesses); scanf("%d%d%d\n", userdig1, userdig2, userdig3); //check user's digits against actual digits if (userdig1 == randdig1) //if first user digit = first actual digit { printf("number %d hit!\n", userdig1); } if (userdig2 == randdig2) //if second user digit = second actual digit { printf("number %d hit!\n", userdig2); } if (userdig3 == randdig3) //if third user digit = third actual digit { printf("number %d hit!\n", userdig3); } if (userdig1 == randdig1 && userdig2 == randdig2 && userdig3 == randdig3) //if 3 user digits = 3 actual digits { printf("congratulations, guessed number %d%d%d\n", userdig1, userdig2, userdig3); break; } if (userdig1 == randdig2 || userdig1 == randdig3) //if first user digit = second or third actual digit { printf("number %d match!\n", userdig1); } if (userdig2 == randdig1 || userdig2 == randdig3) //if second user digit = first or third actual digit { printf("number %d match!\n", userdig2); } if (userdig3 == randdig1 || userdig3 == randdig2) //if third user digit = first or second actual digit { printf("number %d match!\n", userdig3); } else //if none of user's digits matches or hits { printf("none of digits entered matches or hits. guess again.\n"); } } printf("game over! failed guess correct number.\n"); }
after compiling , running, segmentation fault error after enter in first guess.
now know why segmentation faults occur. means trying access memory/values don't have access (something along lines). in particular case, don't understand cause happen. if had venture guess, i'd has scanf
statement, i'm trying "extract" digits interpreting each digit separate integer. know mathematical way extract digits (mod 10, divide 10), i've used concept in different program, not loop logic. have tried use loop extract digits here, have not yet found way works numbers. ultimately, problem here, , can fix it?
any , appreciated.
(additionally, know have bad formatting here, example break
statement being in middle of if
statements. move code separate function user can play again without having close program , reopen it).
because passing integer scanf()
pointer expected.
making pointer integer undefined behavior, should pass address of values because modifed function, correct way use scanf()
in situation is
if scanf("%d%d%d*%c", &userdig1, &userdig2, &userdig3) == 3) input_is_correct(); else input_is_bad();
notice in case passing 0
scanf()
, attempt dereference (void *) 0x00
or null
perhaps. undefined behavior.
using pointer not address of variable taken &
operator or return value of malloc()
/calloc()
/realloc()
in general undefined behavior.
Comments
Post a Comment