K
K
Konstantin Malyarov2016-04-19 23:49:20
C++ / C#
Konstantin Malyarov, 2016-04-19 23:49:20

What does this line mean? Her purpose?

261913937e41423a866425ef7c4de6ab.jpg
The only thing I understand is that the search_for array is equal to the number of characters entered -1 (let's say "green" i.e. 7 - 1 = 6 [due to the fact that the array indexing starts from 0, not 1]). And we add '\0' to the value of the last index .
Why bring it in?
His role?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey, 2016-04-19
@alsopub

The search_for array first reads the user-entered string ending in "Enter" (0x10), then this character Enter is replaced by 0x00, which in C is a sign of the end of the line.
That is, after fgets in the array, we have the entered string, line feed, \0.
After the specified string, the entered string remains in the array, \0, \0 (the second "zero" no longer carries a load).

A
Alexander, 2016-04-20
@Serpentiel

You can find a full explanation of the meaning of "null-terminated string" here Null-terminated string , but in short, in C-like languages, this is a terminating string.

R
res2001, 2016-04-20
@res2001

The operation with the assignment of 0 in this form is completely superfluous here, because fgets in the previous step already wrote 0 at the end of the line. Without this, strlen would not be able to calculate the correct length of the string.
I would also understand something like this: search_for[79]=0; - insurance against buffer overflows, although this is redundant if you rely on the correct operation of fgets. But we probably won't be checking every implementation of fgets, so this statement seems more logical than the one in the example.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question