You can explain what this function does (the first parameter = ' ' , the second is just a word served, in this case a letter, especially not very clear in the while condition, we say that our space is not equal to a letter, but why is it written 2 times?

 char * mystrchr(const char * s, const char c) { while ( *s && *s != c ){ ++s; } return ( *s ) ? (char*) s : NULL; } 
  • Then accept one of the answers by clicking on the check mark to the left of it. At the same time pay attention that the answer @Yaroslav contains very strange and unreasonable recommendations. - Qwertiy

2 answers 2

condition is abbreviated. Completely need to read it so

 *s != '\0' && *s != c 

that is, until the end of the line (there is usually a null character at the end of sish lines) and this is not a necessary character, we move along the line.

And at the very end we check once again what exactly we found. If the character is zero, then the desired character is not found.

  • Can you comment on the next answer? - Qwertiy
  • one
    I would like to add the 1st comment from KoVadim. I myself teach by K & R C, and there somewhere in chapter 5 this was explained: static char *allocp = allocbuf; this is the same as static char *allocp = &allocbuf[0]; since the array name is simultaneously the address of its zero element - KrasPvP

This function searches for the first occurrence of the character c in the string s .

This design

 while (*s && *s != c) 

can be presented in a more readable form

 while ( (*s != NULL) && (*s != c) ) 
  • one
    Well, what's the insecurity? And what is the severity of the second? And where so many brackets, salt them or what? - Qwertiy
  • some compilers can do the operation first (*s && *s) and then check the result with ( (*s && *s) != с) - Yaroslav
  • 2
    What kind? The priority of operations is prescribed in the language standard. - Qwertiy
  • one
    there is such an XLC compiler that it constructs such a construction correctly, but a bit more complicated and not like gcc. Therefore, out of habit I place brackets everywhere. - Yaroslav
  • 2
    @Qwertiy, the world supply of brackets is unlimited, so they can not save. The main thing is that the code is clear to the person (and beautiful). And it is better not to use poor compilers (to @ Yaroslav``) at all. Are there still places in which gcc doesn't work? - avp