使用C代码遍历获得内容的时候,我写了如下的代码

...
MYSQL_RES *mysqlres = mysql_store_result(&conn);
MYSQL_ROW row;
while(row = mysql_fetch_row(mysqlres))
{
....
}

然后gcc报warning

dbcon.cc:103:38: 
warning: suggest parentheses around assignment used as truth value [-Wparentheses]

前文高亮代码意图是, 首先MYSQL_ROW row 赋值为函数mysql_fetch_row的返回值,然后对row的值判断,若非0,则执行while的内容,否则跳出循环. 如有人所说,"It's just a 'safety' warning",前文的内容是正确但不良好的习惯. 有人说这是和

while(row == mysql_fetch_row(mysqlres))

区别开来.不很确定,但无论如何, 这不是一个值得提倡的写法...

消除这个warning很简单,将该行修改为

while((row = mysql_fetch_row(mysqlres)) != NULL)

或者

while((row = mysql_fetch_row(mysqlres)))

参考这篇

Categories: Code

Yu

Ideals are like the stars: we never reach them, but like the mariners of the sea, we chart our course by them.

4 Comments

一季花落 · July 10, 2013 at 02:01

Google Chrome 26.0.1410.64 Google Chrome 26.0.1410.64 Windows 7 x64 Edition Windows 7 x64 Edition

对代码一点也不懂

Leniy · July 10, 2013 at 08:09

Google Chrome 27.0.1453.116 Google Chrome 27.0.1453.116 Windows 7 Windows 7

我有多少年没碰过c了啊

    yu · July 10, 2013 at 12:16

    Google Chrome 28.0.1500.71 Google Chrome 28.0.1500.71 GNU/Linux x64 GNU/Linux x64

    C多好用啊
    上次python实现一个处理大数据的算法,跑了3个多小时,我表示不懂算法但可以用C重写一遍,然后跑了20秒…
    就是这个: https://github.com/DionyBudy/svd_with_parse/blob/master/svd.c

    你有兴趣可以重写回python试试效果…

      Leniy · July 11, 2013 at 17:10

      Google Chrome 27.0.1453.116 Google Chrome 27.0.1453.116 Windows 7 Windows 7

      这封邮件先留着,等考完试试试

Leave a Reply

Your email address will not be published. Required fields are marked *