I have a little test program looking for a way to best compare strings from argv.
If strncmp compares upto 'n' chars unless it hits a '\0', it will return a succesful compare for strings like "foo" against "foobar" as it reaches a '\0' after the second 'o' and exits.
So my question is, what is the safest way to compare 2 full strings, returning an incorrect compare if they are not 100% identical.
Test program:
Code:
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
TCHAR Protocols[][5] = {
_T("IP"),
_T("ICMP"),
_T("TCP"),
_T("UDP")
};
INT _tmain(INT argc, TCHAR *argv[])
{
INT k;
TCHAR Proto[5];
_tcsncpy(Proto, argv[1], sizeof(Proto));
for (k = 0; k < sizeof(Protocols) / sizeof(Protocols[0]); k++)
{
if ( _tcsnicmp( Proto, Protocols[k], _tcslen(Proto) ) == 0)
_tprintf(_T("match found %s\n"), Protocols[k]);
}
return 0;
}
Result:
Code:
c:\> test I
match found IP
match found ICMP
This should not match