Doubt in strcmp

Asked

Viewed 277 times

3

I’m in doubt on a line of code:

if(strcmp(novo->nome,aux->nome)>=0)

How does your comparison occur? Being that the name is a char!

  • Being nome a char, that statement has error. With the #include <string.h> compiler detects error and warns programmer.

  • @pmg error why?

  • @Jorgeb.: Mistake because the guy char is not compatible with the type char *. The function strcmp() compares the content of two pointers; not the value of two characters.

1 answer

3


The strcmp method tests the equality of strings.

  1. Returns a negative number if string1 is less than string2;
  2. Returns zero if the two strings are equal;
  3. Returns a positive number if string1 is greater than string2.

The method declaration is as follows::

int strcmp ( const char * str1, const char * str2 );

So returns an integer.

Soon, in your code, will enter the scope of if when novo->nome is equal to or greater than aux->nome

Browser other questions tagged

You are not signed in. Login or sign up in order to post.