2011-07-17 63 views
0

初始化結構時出現奇怪的編譯器錯誤。使用struct時編譯器錯誤

#include <stdio.h> 
#include <unistd.h> 
#include <stdlib.h> 
#include <string.h> 

struct RadarData 
{ 
    unsigned int messageID : 32; 
    unsigned int time : 32; 
    float az; 
    float el; 
}; 
struct RadarData sendData; 

sendData.az = 25; 
sendData.el = 10; 
sendData.messageID = 1; 
sendData.time = 100; 

這看起來根據幾個不同的教程沒什麼問題,但在兩臺不同的機器,編譯時,我發現了以下錯誤:

testserver.c:15:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘.’ token
testserver.c:16:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘.’ token
testserver.c:17:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘.’ token
testserver.c:18:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘.’ token

爲什麼會出現這個錯誤?

+0

你能粘貼整個代碼片段嗎?包括哪些函數出現在.. – yan

+0

看看行號 - 我認爲這是他真實的代碼(我想他可能暫時忘記了這個函數,但也許不是......) – bdonlan

回答

9
sendData.az = 25; 

像這樣的語句必須在函數內。如果你想初始化結構,有針對不同的語法:

struct RadarData sendData = { 25, 10, 1, 100 }; 
3

如果我在看你的代碼的權利(這是完整的相關代碼),那麼你將語句的功能之外。那是不對的。