What is the difference between assigning and comparing string variables with function or with assignment and comparison operator?

10

I came across the following questions:

  • What is the difference between the expressions strcpy (s, t) e s = t ?
  • What is the difference between the expressions if (strcmp (s, t) < 0) e if (s < t)  ?

I tried to compile the code s=t of the first question, but I got an error. However I can compile if (s<t) , but I noticed by its behavior that it is not similar to the strcmp command. What does if(s<t) mean? Since s and t are vectors of char ( strings )?

The codes that follow were on the same questions from which to extract the quoted questions at the beginning and I'm posting them because they serve as an "illustration" of the question. Home What's wrong with the code segment below?

char b[8], a[8];
strcpy (a, "abacate");
strcpy (b, "banana");
if (a < b)
   printf ("%s vem antes de %s no dicionário", a, b);
else
   printf ("%s vem depois de %s no dicionário", a, b);



char *b, *a;
a = "abacate";
b = "banana";
if (a < b)
   printf ("%s vem antes de %s no dicionário", a, b);
else
   printf ("%s vem depois de %s no dicionário", a, b);



char *a, *b;
a = "abacate";
b = "amora";
if (*a < *b)
   printf ("%s vem antes de %s no dicionário", a, b);
else
   printf ("%s vem depois de %s no dicionário", a, b);
    
asked by anonymous 11.08.2015 / 00:44

2 answers

7
strcpy (s, t)

You are copying content from t to s . This copy happens by to byte of the string . That is, all bytes that are present at the address indicated by t at the end of the operation will also be present at the address indicated by s . Anyway, you'd prefer to use the strncpy() function that is safer.

s = t

You are copying the pointer contained in the variable t to the variable s . Because it is an array the operation is invalid. You are trying to assign a type char * (pointer to character) which is the type of every literal string in C (roughly) to an array of characters, and are different things.

if (strcmp (s, t) < 0)

It compares the bytes of s with the bytes in t and analyzes there are differences between them and the first different character has to be smaller in s than the character found in the same position in t .

if (s < t)

Is comparing if the value of where array of s which is just an address, is less than the value of t which is also an array . As we are talking about a sequence of characters we have to always use the functions. In these cases using the pointer is of no use. Try the ideone how wrong it is. If you do in alphabetical order it works out coincidentally but if it is not in order it goes wrong because the comparison is with the pointer and then the allocation order is worth it.

In the codes shown at the end, you can assign a value to the variable because it is a pointer and does not give an error precisely because the type is compatible with the literal. in this case it works because you're only working with literals.

But you are making a serious mistake in other situations because there is no space allocated for these variables. Variables pointers only hold the pointer, the address of where the object will be. The memory at this address should be allocated through the malloc() . And then it should be released with free() . This allocation will return the address from which the memory was allocated and is its pointer. In your example it works because the literal was allocated by the compiler and this address was used. If you try to change this content, you'll have problems.

The last example starts by going the right way because instead of comparing the pointers, it compares the contents, since the * operator indicates to pick up the existing value at the address pointed by the variable. The only problem is that this comparison occurs only in the first character of the string . The comparison of this form is basically numerical, it is not made in the sequence of characters. The computer can not traverse the sequence on its own, an algorithm is needed to do this. And in this case the operation would only be correct if you compared all the string , and this is done with the already known strcmp() .

    
11.08.2015 / 01:43
5

As a vector is an element that occupies contiguous positions in RAM memory, just save the memory address of the first position of the same. The remainder can be obtained by basic arithmetic with pointers, see:

For this example consider the following variable char b [8] .

Therefore:

b[1] = 'c';

is the same as doing:

*(b+1) = 'c';

The second case, at first, may seem strange, but what is being done is quite simple.

(1 + b) means to move 1 byte of memory from the beginning of the vector (stored in the b pointer). The result of (b + 1) is a pointer (a memory address) of position 1 of the vector (same as b [1]). To place a value in this position, you must use the * operator, with which you can access the contents of the memory address stored in the pointer. So you have to do * (b + 1).

In other words, (b + 1) is the sum of a memory address (saved by the variable b ) with 1 byte. Note that it is 1 byte because it is a variable of type char. If the vector is int b [10] , then the sum (b + 1) would shift 4 bytes (32-bit architecture) from the beginning of the vector. Since C is a strongly typed language, the compiler itself knows how many bytes it will have to move (by type of variable).

See another way to do the same:

char * p;
p = b;
p[1] = 'c';

or

char * p;
p = b;
*(p+1) = 'c';

The above code shows that you can assign a vector of the same type to a pointer.

Therefore, a variable of a vector in C is actually a pointer to the 1st position of that vector, and it is for this reason that doing if (s < t) is not the even if if (strcmp (s, t)

11.08.2015 / 05:43