2013-04-25 49 views
2

我有以下代码:解决方案编译器警告:控制到达非void函数结束

int cons_col() 
{ 
for(int col =0; rx_state_== MAC_IDLE; col++) 
return col; 
} 

它就像一个应该返回一个整数,当条件rx_state_ == MAC_IDLE满足计数器; 当我编译时,我得到警告:控制达到非void函数的结束。

请问这个问题消失,如果添加以下在上面的函数的末尾:

if (coll == 0) 
return 0; 

感谢

+0

你的功能,你想让它工作,只是返回0 – 2013-04-25 05:43:33

回答

5

该代码计算了这一点。

int cons_col() 
{ 
    for(int col = 0; rx_state_ == MAC_IDLE; col++) 
    { 
     return col; 
     // "return" prevents this loop from finishing its first pass, 
     // so "col++" (above) is NEVER called. 
    } 
    // What happens here? What int gets returned? 
} 

注意,此函数始终立即完整。

它这样做是:

  • 设置整col0
  • 支票一次如果rx_state_MAC_IDLE
  • 如果是,则返回0
  • 如果没有,它到达// What happens here?,然后到达非void函数结束而不返回任何东西。

从你的描述中,你可能想要这样的东西。

int cons_col() 
{ 
    int col = 0; 
    for(; rx_state_ != MAC_IDLE; col++) 
    { 
     // You may want some type of sleep() function here. 
     // Counting as fast as possible will keep a CPU very busy 
    } 
    return col; 
} 
+0

'检查是否0也是你MAC_IDLE.'can解释这一点吗? – 2013-04-25 05:47:47

+0

@Koushik Typo就我而言! :-) – 2013-04-25 05:52:51

+0

啊谢谢。 +1 :-) – 2013-04-25 05:54:29

相关问题