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

Asked

Viewed 1,506 times

11

I came across the following questions:

  • What is the difference between expressions strcpy (s, t) e s = t ?
  • What is the difference between 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 your behavior that is not similar to command strcmp. What does it mean then if(s<t)? Since s and t are vectors of char (strings)?

The codes that follow were in the same questions from which to extract the questions quoted at the beginning and I am posting them because they serve as an "illustration" of the question.
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);
  • Someone who understands of C could improve the title of this question? This way it is not clear the doubt, but the question seems interesting.

2 answers

7


strcpy(s, t)

You’re copying the contents of t in s. This copy happens byte to byte of string. That is, all the bytes which 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 prefer to use the function strncpy() which is safer.

s = t

Is copying the pointer contained in the variable t for the variable s. How is a array the operation is invalid. You are trying to assign a type char * (pointer to character) which is the whole literal type string in C (roughly) to a array of characters, and they’re different things.

if (strcmp(s, t) < 0)

Makes the comparison of bytes of s with the bytes existing 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 whether the value of where is the array of s which is only an address, is less than the value of t which is also an address of array. As we are talking about a string we have to always use the functions. In these cases using the pointer has no use. Test in ideone how wrong it gets. If you do it alphabetically it works by coincidence but if it’s not in order it goes wrong because the comparison is with the pointer and then the allocation order is that it counts.

I put in the Github for future reference.

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

But you will be making a serious mistake in other situations because there is no space allocated for these variables. Pointer variables only store the pointer, the address where the object will be. The memory at this address must be allocated through the function malloc(). And then it must 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 will have problems.

The last example starts going the right way because instead of comparing the pointers, it compares the content, since the operator * indicates to take the existing value in 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 done in the string. The computer can’t run the sequence on its own, it needs an algorithm to do this. And in this case the operation would only be correct if it compared the whole string, and this is done with the already known strcmp().

5

Since a vector is an element that occupies contiguous positions in RAM memory, it is sufficient to store the memory address of the first position of the same. The rest 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, initially, may seem strange, but what is being done is quite simple.

(1+b) means to displace 1 byte of memory from the beginning of the vector (stored in the pointer b). The result of (b+1) is a pointer (a memory address) of position 1 of the vector (the same as b[1]). To put a value in this position, you need to use the *operator, with it you can access the content of the memory address stored in the pointer. That is why *(b+1).

In other words, (b+1) is the sum of a memory address (saved by variable b) with 1 byte. Note that it is 1 byte, because it is a char variable. If the vector were int b[10], then the sum (b+1) would displace 4 bytes (architecture 32 bits) 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 variable type).

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 a pointer can be assigned a vector of the same type.

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 we do this if (s < t) is not the same as if (strcmp(s, t) < 0). In the first case the memory address of s and t. In the second case, a function is being used that iterates (byte to byte) over these two vectors in order to compare whether one is equal to the other in relation to the stored bytes.

Browser other questions tagged

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