What does `(c = * str)! = 0` mean?

int equiv (char, char);
int nmatches(char *str, char comp) {
    char c;
    int n=0;
    while ((c = *str) != 0) {  
       if (equiv(c,comp) != 0) n++;
      str++;
    }
    return (n);    
}

What does "(c = * str)! = 0" really mean? Can someone please explain this to me or give me the right conditions to search for the explanation itself?

+4
source share
7 answers

This expression consists of two parts:

  • c = *str- this is a simple assignment cfrom dereferencing a pointer,
  • val != 0 is a comparison with zero.

, , . , char, . , , , , char c .

, != 0 C, while :

while ((c = *str)) {
    ...
}

, , , . , , c = *str, c == *str, . .

+5

,

while ((c = *str) != 0) {

while (c = *str) {

*str c, *str \0; .. .

, , (, c == *str), C ++, .

+2

(c = *str) . , - . (c = *str) *str.

, *str, c, 0. , equiv .

0 . , .

+1

str, c, , c 0, .

'\0', , , NUL.

+1

str while char, - char.

"for":

 for (int i = 0; i < strlen(str); ++i )
       std::cout << str[i];
0

. , str c, , .

C '\0':

if(c != '\0')

, C - escape- \0.


- . 1980- , , ", " . , , . , = ==.


:

c = *str;
while (c != '\0')
{
  if(equiv(c, comp) != 0)
  {
    n++;
  }
  str++;
  c = *str;
}
0

You do not need to char c, since you already have a pointer char *str, you can also replace != 0with != '\0'for better readability (if not compatibility)

while (*str != '\0')
{  
    if (equiv((*str),comp)
          != 0)
    { n++; }

    str++;   
}

To understand what the code does, you can read it like this:

while ( <str> pointed-to value is-not <end_of_string> )
{
    if (function <equiv> with parameters( <str> pointed-to value, <comp> )
            returned non-zero integer value)
    then { increment <n> by 1 }

    increment pointer <str> by 1 x sizeof(char) so it points to next adjacent char
}
0
source

Source: https://habr.com/ru/post/1673488/


All Articles