0

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 :)

Community
  • 1
  • 1

2 Answers2

1
  • 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.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • Hi again Deduplicator I have trouble dual booting, which is why I use VS2013, hence no plaintext compiler options. Anything more concrete that would help me solve this? – Wouldn't You Like To Know Apr 05 '14 at 18:29
  • Hm, i'm quite sure that IDE shows the compiler options somewhere. Look at project properties. Also, did you already get [C99 with Technical corrigenda TC1, TC2, and TC3 included](http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf) for reference? Be aware though, the MS compiler is not a proper C compiler, by design. – Deduplicator Apr 05 '14 at 18:31
1

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!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • Setting Intermediary to a char * gives me an error earlier on in the program, one that I'd rather not dissemble. I'm attempting a port of the Python code found here: https://github.com/brownan/Rubiks-Cube-Solver/blob/master/cube_convert.py , specifically the code between lines 90 and 122. – Wouldn't You Like To Know Apr 05 '14 at 18:40
  • That means something else is wrong in your code. I assure you that your program will not work correctly as long as you are passing a `char` as a first parameter to a function that needs a `char*`. – templatetypedef Apr 05 '14 at 18:50
  • https://github.com/bsparks100/RubiksCubeSolver/blob/master/StringConverter.c Could you please review my sorry excuse for C code here, and propose corrections over Github? – Wouldn't You Like To Know Apr 05 '14 at 19:04