Compare a vector of char?

0

Good people I have the following doubt why I can not compare two vectors of type char using relational operators? I know that vectors are a compound type and that the characters are in a static area am I right?

But what influences at the time of the comparison knowing that vector is composed and that the string is in a static area?

    
asked by anonymous 21.04.2018 / 21:10

1 answer

1

"Can not" compare with relational operators because the variables representing the vectors are pointers to their first elements.

For this reason when comparing for example with == you are comparing whether the pointers have the same value, that is, they point to the same location in memory. This will only happen if they are the same vector. Regarding whether or not they are in a static area will not influence the comparison at all.

Consider the following example:

char texto1[] = "um texto";
char texto2[] = "um texto";

printf("\n%d", texto1 == texto2); //0 - que representa falso

char *texto3 = texto1;
printf("\n%d", texto1 == texto3); //1 - que representa verdadeiro

In the last example texto3 was a pointer that I put manually pointing to the texto1 vector.

In contrast, comparing with <= or >= will only tell you if one pointer is larger than the other, which is not useful.

The comparison should then be made with a loop / loop, which passes to each letter and compares each vector. Using a very simple version could do so:

int compara_strings(char str1[], char str2[]){
    int i;
    for (i = 0; str1[i] != '
char texto1[] = "um texto";
char texto2[] = "um texto";

printf("\n%d", strcmp(texto1,texto2)); //0 - que representa iguais
' && str2[i] != '
char texto1[] = "um texto";
char texto2[] = "um texto";

printf("\n%d", texto1 == texto2); //0 - que representa falso

char *texto3 = texto1;
printf("\n%d", texto1 == texto3); //1 - que representa verdadeiro
'; i++){ //assim que uma letra seja diferente if (str1[i] != str2[i]){ //retorna -1 se a primeira for menor ou 1 caso contrário return str1[i] < str2[i] ? -1 : 1; } } //Se as duas acabaram, são iguais e retorna 0. Se a str1 acabou e str2 não //retorna um valor negativo. Se str2 acabou e a str1 retorna um valor positivo return str1[i] - str2[i]; }

Notice that here the returns have specific meanings:

  • 0 - equal strings
  • negative - first string smaller than second
  • positive - first string greater than second

When it is said that a string is higher or lower, it is a lexicographic (alphabetical) comparison.

However I would be reinventing the wheel because there is already a function to make this same comparison in <string.h> which is called strcmp .

Using this function in the initial example would be:

int compara_strings(char str1[], char str2[]){
    int i;
    for (i = 0; str1[i] != '
char texto1[] = "um texto";
char texto2[] = "um texto";

printf("\n%d", strcmp(texto1,texto2)); //0 - que representa iguais
' && str2[i] != '%pre%'; i++){ //assim que uma letra seja diferente if (str1[i] != str2[i]){ //retorna -1 se a primeira for menor ou 1 caso contrário return str1[i] < str2[i] ? -1 : 1; } } //Se as duas acabaram, são iguais e retorna 0. Se a str1 acabou e str2 não //retorna um valor negativo. Se str2 acabou e a str1 retorna um valor positivo return str1[i] - str2[i]; }
    
21.04.2018 / 22:51