segmentation fault !!!

May 16, 2020 at 5:13pm
hey,
In the main part, I get the segmentation fault error while going to the next part of this,
for (int j = 0; j < strlen(*guess); j++) {

for (int z = 0; z < strlen(*guess); z++) {
if (*guess[j] == *secret_number[z])
cows++;
}
if (*guess[j] == *secret_number[j])
bulls++;

}
Last edited on May 17, 2020 at 12:00pm
May 16, 2020 at 5:16pm
1
2
int comparenum;
comparenum = (int)malloc(16);


This makes no sense. What are you trying to do here. This makes no sense.
May 16, 2020 at 5:22pm
yes I removed that part but the result did not change I rearranged my question
May 18, 2020 at 3:44pm
Presumably, you're accessing invalid memory. Either you're dereferencing a pointer to invalid memory, or you're trying to access beyond the memory allocated for one of your arrays.

You really haven't shown us enough code for us to know.
May 18, 2020 at 3:54pm
It depends entirely on how you have your memory laid out.

What exactly is guess?
if strlen(*guess) compiles, that means that guess is a char** of some sort.

guess[i] might not even be valid data, but we can't tell without knowing more.
You might have meant to do (*guess)[i] or guess[0][i] to access individual characters, but it's very odd.
Last edited on May 18, 2020 at 3:55pm
May 18, 2020 at 5:59pm
Get rid of all this pointer stuff and stick with char arrays.
I guess this is related to a previous topic about cows and bulls.
Topic archived. No new replies allowed.