首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

一个有关return的有关问题

2012-03-01 
一个有关return的问题voidDoNewWord(BufferSocketBufSock){charbuf[BUFFER_SIZE],*NotFind Sorry,Wecan

一个有关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 。

热点排行