一个有关return的问题
void DoNewWord(BufferSocket BufSock)
{
char buf[BUFFER_SIZE],*NotFind= "Sorry,We can 't find the word in the dictionary! ";
int Ret;
Ret = ReadLineFromBufferSocket(&BufSock , buf , WORDLEN);
buf[Ret-2]= '\0 ';
int index=SearchWordByHash(buf,DictNum);
if(index!=-1)
{
if((Ret=WriteSocket(BufSock.Socket,dictionary[index].mean,strlen(dictionary[index].mean))) < 0 )
printf( "Send failed.You can try again!\n ");
}
else
{
if((Ret=WriteSocket(BufSock.Socket,NotFind,strlen(NotFind))) < 0 )
printf( "Send failed.You can try again!\n ");
}
return;
}
int SearchWordByHash(char *word,int num)
{
int temp;
temp=13
if(!strcmp(dictionary[temp].data,word))
return temp;//如果相等,这里返回一个temp
else
{
for(temp=0;temp <DictNum;temp++)
if(!strcmp(dictionary[temp].data,word))
return temp;//如果不等,dictionary[0].data和word相等,返回0
}
return -1;//问题是这里的return -1是什么情况下返回?
}
[解决办法]
for(temp=0;temp <DictNum;temp++)
if(!strcmp(dictionary[temp].data,word))
return temp;//如果不等,dictionary[0].data和word相等,返回0
以上循环执行完了都没有找到对应的数据,就开始执行下面的语句.
return -1;
[解决办法]
程序执行过程是这样的:
当 dictionary[] 中索引为13处的数据与word相同时[if(!strcmp(dictionary[temp].data,word))],直接返回索引号(13),否则就在整个字典中查找word,若word在字典中存在,则返回对应的索引号,否则就返回 -1 。