C Script Prematurely Terminating

Joined
Feb 7, 2022
Messages
2
Reaction score
0
I'm quite new to C, and I was creating a simple Wordle game. It seems to work pretty well with most words, but when the word I guess is not found in the word the script stops running. I can't find any logical reason that the code would stop, does anyone know why it's stopping? Thanks!

Here is my code:
#include <stdio.h>
#include <string.h>

int main()
{
char guess[20];
char word[] = "henlo";
int loopNumber = 0;
int loopNumber2 = 0;
char *s;
while (1 != 2){
loopNumber2 = 0;
printf("Enter your guess: ");
scanf("%s",guess);
if (strcmp(guess, word) == 0){
printf("You guessed on guess number %d!\n",loopNumber+1);
break;
}
else {
while (1 != 2){
s = strchr (word, guess[loopNumber2]);
if (guess[loopNumber2] == word[loopNumber2]){
printf("Letter number %d is green!\n",loopNumber2+1);
}
else if (strcmp(s, "null") == 0){
printf("Letter number %d is red!\n",loopNumber2+1);
}
else {
printf("Letter number %d is yellow!\n",loopNumber2+1);
}
loopNumber2 = loopNumber2 + 1;
if (loopNumber2 == 5){
break;
}
}
}
loopNumber = loopNumber + 1;
if (loopNumber == 6){
printf("You weren't able to get it this time. :(");
break;
}
}
printf("do we get here?");
return 0;
}
 
Joined
Mar 3, 2021
Messages
240
Reaction score
30
It's your check if s is NULL. If a letter isn't found, the value of s is NULL. Not a string of that, the actual value NULL, which is a pointer to memory position zero. Since you don't own that memory, you get a segfault the first time a letter is checked that isn't in the word. Change that line to the following and it should work.
C:
else if (s == NULL){
I've got other advice, too, if you want to clean up your code a little. Keep at it!
 
Joined
Mar 3, 2021
Messages
240
Reaction score
30
Sorry, I was in a bit of a rush last night. An excellent way of debugging sefaults is using Valgrind. With the right options, it'll show you the exact line that's poking around where it shouldn't. I compiled your code using gcc -g <src>.c. The -g option adds debug information to the binary it creates. Then, valgrind --tool=memcheck --leak-check=full --track-origins=yes ./a.out (a.out being the default binary name for gcc). And this is what I got out:

Code:
[cmj@lunitari:~/workspace/c_test]$ gcc -g jackNewport_wordl.c
[cmj@lunitari:~/workspace/c_test]$ valgrind --tool=memcheck --leak-check=full --track-origins=yes ./a.out
==1864661== Memcheck, a memory error detector
==1864661== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==1864661== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info
==1864661== Command: ./a.out
==1864661==
Enter your guess: oij
Letter number 1 is yellow!
==1864661== Invalid read of size 1
==1864661==    at 0x483DAB4: strcmp (vg_replace_strmem.c:847)
==1864661==    by 0x401238: main (jackNewport_wordl.c:26)
==1864661==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==1864661==
==1864661==
==1864661== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==1864661==  Access not within mapped region at address 0x0
==1864661==    at 0x483DAB4: strcmp (vg_replace_strmem.c:847)
==1864661==    by 0x401238: main (jackNewport_wordl.c:26)
==1864661==  If you believe this happened as a result of a stack
==1864661==  overflow in your program's main thread (unlikely but
==1864661==  possible), you can try to increase the size of the
==1864661==  main thread stack using the --main-stacksize= flag.
==1864661==  The main thread stack size used in this run was 8388608.
==1864661==
==1864661== HEAP SUMMARY:
==1864661==     in use at exit: 0 bytes in 0 blocks
==1864661==   total heap usage: 2 allocs, 2 frees, 2,048 bytes allocated
==1864661==
==1864661== All heap blocks were freed -- no leaks are possible
==1864661==
==1864661== For lists of detected and suppressed errors, rerun with: -s
==1864661== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)

Granted, it's a bit verbose and might take some getting used to to read through it all, but I can't tell you how invaluable it is. Right below the "Letter number 1 is yellow!" output (Valgrind and your program's stdout get mixed), there's the "Invalid read of size 1", which then tells you exactly where it happened. Generally, start at the bottom of that backtrace and work your way upwards. Eventually, it'll start going into files that aren't yours. But, plain as day, by 0x401238: main (jackNewport_wordl.c:26), shows the line that's trying to compare s. As mentioned earlier, NULL is the location at zero. Or, in hex, 0x0, which is mentioned right above the location in source.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top