C String strncmp difficulty

Pages: 123
Sep 3, 2013 at 11:18pm
That's the point of strncmp. You set the third parameter to strlen(<string2>).

What you just described is WHY strcmp is unsafe.
Sep 3, 2013 at 11:21pm
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 Sep 4, 2013 at 12:06am
Sep 3, 2013 at 11:23pm
@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 Sep 3, 2013 at 11:26pm
Sep 3, 2013 at 11:28pm
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 Sep 3, 2013 at 11:28pm
Sep 3, 2013 at 11:28pm
@andywestken

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


It looks like a next stupidy.
Sep 3, 2013 at 11:29pm
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 Sep 3, 2013 at 11:54pm
Sep 3, 2013 at 11:30pm
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.
Sep 3, 2013 at 11:31pm
@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.
Sep 3, 2013 at 11:33pm
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.
Sep 3, 2013 at 11:34pm
@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.
Sep 3, 2013 at 11:37pm
@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 Sep 3, 2013 at 11:38pm
Sep 3, 2013 at 11:38pm
5
Sep 3, 2013 at 11:39pm
it's actually 4

strlen does not include the null.
Last edited on Sep 3, 2013 at 11:40pm
Sep 3, 2013 at 11:40pm
@ciphermagi

5


I am sorry but you have not passed the test.:)
Sep 3, 2013 at 11:40pm
A simple question for beginner vlad:
What value will return for '\0' == ' '?
Sep 3, 2013 at 11:41pm
ciphermagi:

The point vlad is making is that 0 == ' ' will never be checked, because it stops after the 4th character.
Sep 3, 2013 at 11:42pm

@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.
Sep 3, 2013 at 11:43pm
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.
Sep 3, 2013 at 11:48pm
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 Sep 3, 2013 at 11:54pm
Sep 3, 2013 at 11:48pm
@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