Topic: Possible strcmp bug revisited
Author: oi33nn@usun01.d10.pdb.sni.de (Herr Henrich)
Date: Tue, 10 May 1994 14:21:02 GMT Raw View
kevlin@wslint.demon.co.uk (Kevlin Henney) writes:
>In article <dreamersCop0FH.LqK@netcom.com>
> dreamers@netcom.com "Earl and Carmella Brown" writes:
>>Can anyone please help me out? I just realized this after 5 years of
>>programming in C and C++:
>>
>>#define NUM_WORDS 2
>>char* paszWords[NUM_WORDS]= {"Read", "Reading"};
>>....
>>/*
>> This function will find the index of a given word in the list
>>paszWords. This list can be easily modified, and the index value could be
>>used as a command ID.
>>-1 indicates word not found.
>>*/
>>
>>int GetWordIndex(char* pszWordToTest)
>>{
>> int index = NUM_WORDS - 1;
>> while ( (index >=0) &&
>> (strcmp(paszWords[index], pszWordToTest) != 0x00) ) {
>> index++;
^^^
use -- instead of ++ :-)
>> }
>> return index;
>>}
>>
>>....
>>// pszCommand = "Read"; hypothetically...
>> GetWordIndex(pszCommand);
>>
>>/* end code fragment!!! */
>>
>>Given this situation, I will get index of 1 every time! I've tried it, and
>[stuff deleted]
>Is there any reason that you are starting your array search from 1
>rather than 0?
Bingo !
>--
>Kevlin Henney
--
/-----------------------------------------------\
Dirk Henrich | DISCLAIMER: Opinions presented here are MINE, |
henrich.muc@sni.de (Eunet) | possibly *NOT* those of Siemens Nixdorf !!!!! |
henrich.muc@sni-usa.com | Indeed, I didn't even ask them for theirs ;-) |
\-----------------------------------------------/