Question

Up until this point, my program works flawlessly. But upon reaching the following piece of code, it errors out, giving me a RAV.

char Intermediary[121] = "nnbyonnnnyonnnnyognnbynnnnnyngnrbynnnrnynnnrnyngnnbnonnnnnognrbnnnnrnnngwnbnonwnnnonwnnnogwnbnnnwnnnngwrbnnnwrnnnnwrnnng";
char* result[1024] = { "" };


for (i = 0; i < 120; i++)
{
    if (strchr(Intermediary[i], "y") && strchr(Intermediary[i], "b") && strchr(Intermediary[i], "o"))
        //Conditions passes, set result
        concat(result, Intermediary[i]);
    else
    {
        break;
    }

}

if (i == 120)
{
    // No condition passed: throw an error
    printf("Error: Condition failed :(\n");
    exit(1);
}

printf("%s", result);
getchar();

return 0;

The code for concat can be found here, posted by David Heffernan: How do I concatenate two strings in C?

Thank you in advance :)

Was it helpful?

Solution

The signature of strchr is

char* strchr(char* string, int elem);

This means the string should be first and the element second. When you write

strchr(Intermediary[i], "y")

You're passing a char as the first parameter and a char * as a second parameter. This will result in the code definitely not doing what you think it does.

Did you mean to write

strchr(Intermediary + i, 'y')

which means "search the string Intermediary, offset by i steps, for the character y?

Hope this helps!

OTHER TIPS

  • Do not use magic numbers: Elements in array: (sizeof arr/sizeof *arr).
  • No compiler warning for strchr()? I'm disappointed. Always use compiler options -Wall -Wextra, and handle all warnings appropriately.
  • That loop leaks like there's no tomorrow.

Probably many more errors.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top