C String strncmp difficulty

Pages: 123
That's the point of strncmp. You set the third parameter to strlen(<string2>).

What you just described is WHY strcmp is unsafe.
These functions are not deprecated in or by the standard, therefore they are not deprecated.

Things are deprecated by someone/some company.

Microsoft has deprecated them, so they are deprecated (by Microsoft)

Though not by the standard.

As they are deprecated by Microsoft they are not totally un-deprecated.

Whether you agree with it or not, this still holds.

Andy

Edit: point about deprecation holds, but it's strcpy, strcat, ... nor strcmp I was thinking about.
Last edited on
@ciphermagi

That's the point of strncmp. You set the third parameter to strlen(<string2>).

What you just described is WHY strcmp is unsafe.


Do you have understood what I wrote. Using in this example of strncmp results in a bug.

For example "exit" and "exit poll" are two different strings that are not equal to each other.
Last edited on
No, vlad, it doesn't.
strncmp(str1, str2, strlen(str2);
Using your example, str = "exit" and str2 = "exit poll"
Step 1:
e == e (true)
Step 2:
x == x (true)
Step 3:
i == i (true)
Step 4:
t == t (true)
Step 5:
\0 == ' ' (false)

So you have the right answer.
Last edited on
@andywestken

As they are deprecated by Microsoft they are not totally un-deprecated.


It looks like a next stupidy.
It's only a bug if you make the mistake of subtracting one from the value returned by sizeof()

Andy


Edit -- need to be more awake...
Last edited on
Things are deprectaed by someone/some company.

Microsoft has deprecated them, so they are deprecated (by Microsoft)


If this were a Microsoft compiler forum, I might agree, but it is a C++ forum and these functions are not deprecated in C++ or C, despite anything Microsoft may have to say about it.
@ciphermagi

No, vlad, it doesn't.
strncmp(str1, str2, strlen(str2);
Using your example, str = "exit" and str2 = "exit poll"


Step 1:
e == e (true)
Step 2:
x == x (true)
Step 3:
i == i (true)
Step 4:
t == t (true)
Step 5:
\0 == ' ' (false)


So you have the right answer.


Before to argue with me you should learn what function strlen returns.
It returns a parameter size_t, which is the parameter that strncmp asks for. You're making yourself sound worse and worse all the time.
@cire

If this were a Microsoft compiler forum, I might agree, but it is a C++ forum and these functions are not deprecated in C++ or C, despite anything Microsoft may have to say about it.


Even in a Microsoft compiler forum I would not agree that the function is depreciated. I think that already many users of the compiler wrote angry letters to Microsoft about this stupidy.
@ciphermagi

It returns a parameter size_t, which is the parameter that strncmp asks for. You're making yourself sound worse and worse all the time.


A simple question for beginner @ciphermagi what value will strlen return for string literal "exit"?
Last edited on
5
it's actually 4

strlen does not include the null.
Last edited on
@ciphermagi

5


I am sorry but you have not passed the test.:)
A simple question for beginner vlad:
What value will return for '\0' == ' '?
ciphermagi:

The point vlad is making is that 0 == ' ' will never be checked, because it stops after the 4th character.

@ciphermagi

A simple question for beginner vlad:
What value will return for '\0' == ' '?


Do not you know do you?:) The result will be false because '\0' and ' ' are two different characters.
That's not true, Disch.

His question does illustrate one important thing.

He doesn't know that it's possible to pass in a different string than your control string. Regardless of the length of the control test, the function will STILL work, because it keeps evaluating until it's hit the number of characters defined.
Look again at my post. There should have been 9 steps, because the string passed to srtlen was "exit toll", not "exit". That's not even complete code, because I would actually define it more like this:
1
2
3
4
5
#define MAX(x, y) ((x) >= (y) ? (x) : (y))
..
..
..
isValid = strncmp(str1, str2, MAX(strlen(str1), strlen(str2)));
Last edited on
@ciphermagi

He doesn't know that it's possible to pass in a different string than your control string. Regardless of the length of the control test, the function will STILL work, because it keeps evaluating until it's hit the number of characters defined.


And the result will be a program bug.:) One more investigate the example with "exit" and "exit poll" I showed above.
Pages: 123