求助:if语句的两种方法出现不同结果,请指点,谢谢
今天编写多线程C程序的时间,编写了如下语句:
方式1:
errcode=pthread_create(&(dbthread[inthID[i]]),NULL,net159db,(void *)&inthID[i]);
if(errcode!=0)
{
fprintf(logfile,"Create @@@@@@@@ %d Pthread_create TCP:%s\n",inthID[i],strerror(errcode));
fflush(logfile);
//exit( 1);
}
方式2:
if( (errcode=pthread_create(&(dbthread[inthID[i]]),NULL,net159db,(void *)&inthID[i]) ))
{
fprintf(logfile,"Create @@@@@@@@ %d Pthread_create TCP:%s\n",inthID[i],strerror(errcode));
fflush(logfile);
//exit( 1);
}
这两种方式,其中方式1 不执行括号里面的输出语句,而方式2却执行括号里面的输出语句。
但是在实际上,方式2中,pthread_create应该返回的是0吧?
那么if(errcode=0)应该不执行才对吧?这与实际运行情况不符合。
请各位给指点一下,看看具体是什么原因吧。谢谢了
期待各位的回复,谢谢
[解决办法]
方式1:
errcode=pthread_create(&(dbthread[inthID[i]]),NULL,net159db,(void *)&inthID[i]);
if(errcode!=0) //不同之处就是errcode!=0 用的是!=
方式2:
if( (errcode=pthread_create(&(dbthread[inthID[i]]),NULL,net159db,(void *)&inthID[i]) )) )
没用!=
改成if( !(errcode=pthread_create(&(dbthread[inthID[i]]),NULL,net159db,(void *)&inthID[i]))) )
就一样了
[解决办法]