2011-09-05 147 views
0

編譯器是在MPLAB 8 GCC,dsPIC33系列版本3.30c缺少括號圍繞初始化

// Note: the first block below generates no error's with the compiler, but the data 
// in the structurer is wrong. 
// the 2nd block give warning, but the data is correct... why?? 
//  NMEA2000.c:289: warning: missing braces around initializer 
//  NMEA2000.c:289: warning: (near initialization for 'static_can_filter[0]') 

const tSCF static_can_filter[] = { 
    {(uint32) 0x01FFFFFF}, {(uint8) 2}, // Filter 0 (Process data) 
    {(uint32) 0x00000000}, {(uint8) 2}, // Filter 1  
    {(uint32) 0x00000000}, {(uint8) 2}, // Filter 2 (receive P2P-Message) 
    {(uint32) 0x0000FF00}, {(uint8) 2}, // Filter 3 (receive P2P-Message) 
    {(uint32) 0x00000000}, {(uint8) 2}, // Filter 4 
    {(uint32) 0x00000000}, {(uint8) 2}, // Filter 5 
    {(uint32) 0x00000000}, {(uint8) 2}, // Filter 6 
    {(uint32) 0x00000000}, {(uint8) 2}, // Filter 7 
    {(uint32) 0x00000000}, {(uint8) 2}, // Filter 8 
    {(uint32) 0x00000000}, {(uint8) 2}, // Filter 9 
    {(uint32) 0x00000000}, {(uint8) 2}, // Filter 10 
    {(uint32) 0x00000000}, {(uint8) 2}, // Filter 11 
    {(uint32) 0x00000000}, {(uint8) 2}, // Filter 12 
    {(uint32) 0x00000000}, {(uint8) 2}, // Filter 13 
    {(uint32) 0x00000000}, {(uint8) 2}, // Filter 14 
    (uint32) 0x00000000}, {(uint8) 2} // Filter 15 
}; 


const tSCF static_can_filter[] = { 
    (uint32) 0x01FFFFFF, (uint8) 2, // Filter 0 (Process data) 
    (uint32) 0x00000000, (uint8) 2, // Filter 1  
    (uint32) 0x00000000, (uint8) 2, // Filter 2 (receive P2P-Message) 
    (uint32) 0x0000FF00, (uint8) 2, // Filter 3 (receive P2P-Message) 
    (uint32) 0x00000000, (uint8) 2, // Filter 4 
    (uint32) 0x00000000, (uint8) 2, // Filter 5 
    (uint32) 0x00000000, (uint8) 2, // Filter 6 
    (uint32) 0x00000000, (uint8) 2, // Filter 7 
    (uint32) 0x00000000, (uint8) 2, // Filter 8 
    (uint32) 0x00000000, (uint8) 2, // Filter 9 
    (uint32) 0x00000000, (uint8) 2, // Filter 10 
    (uint32) 0x00000000, (uint8) 2, // Filter 11 
    (uint32) 0x00000000, (uint8) 2, // Filter 12 
    (uint32) 0x00000000, (uint8) 2, // Filter 13 
    (uint32) 0x00000000, (uint8) 2, // Filter 14 
    (uint32) 0x00000000, (uint8) 2 // Filter 15 
}; 
+1

是否定義了「tSCF」? –

回答

0

看起來你在這行的開頭錯過了{

(uint32) 0x00000000}, {(uint8) 2} // Filter 15 
+0

他承認在代碼中(請參閱頂部的註釋) –

0

大概一個未關閉在文件中撐起更高的位置......我們需要在上下文中看到它 - 將文件放在它自己的文件中,第二個數組編譯得很好。我改變了使用查找和替換的類型,這不會產生錯誤或警告與

gcc -c -Wall -Wextra 

所以,錯誤必須在別的地方。

#include <stdint.h> 
const uint32_t static_can_filter[] = { 
    (uint32_t) 0x01FFFFFF, (uint8_t) 2, // Filter 0 (Process data) 
    (uint32_t) 0x00000000, (uint8_t) 2, // Filter 1  
    (uint32_t) 0x00000000, (uint8_t) 2, // Filter 2 (receive P2P-Message) 
    (uint32_t) 0x0000FF00, (uint8_t) 2, // Filter 3 (receive P2P-Message) 
    (uint32_t) 0x00000000, (uint8_t) 2, // Filter 4 
    (uint32_t) 0x00000000, (uint8_t) 2, // Filter 5 
    (uint32_t) 0x00000000, (uint8_t) 2, // Filter 6 
    (uint32_t) 0x00000000, (uint8_t) 2, // Filter 7 
    (uint32_t) 0x00000000, (uint8_t) 2, // Filter 8 
    (uint32_t) 0x00000000, (uint8_t) 2, // Filter 9 
    (uint32_t) 0x00000000, (uint8_t) 2, // Filter 10 
    (uint32_t) 0x00000000, (uint8_t) 2, // Filter 11 
    (uint32_t) 0x00000000, (uint8_t) 2, // Filter 12 
    (uint32_t) 0x00000000, (uint8_t) 2, // Filter 13 
    (uint32_t) 0x00000000, (uint8_t) 2, // Filter 14 
    (uint32_t) 0x00000000, (uint8_t) 2 // Filter 15 
}; 
4

假設TSCF定義類似如下:

typedef struct tSCF { 
    uint32 a; 
    uint8 b; 
} tSCF; 

你想要什麼可能看起來如下:

const tSCF static_can_filter[] = { 
    { (uint32) 0x01FFFFFF, (uint8) 2 }, // Filter 0 (Process data) 
    { (uint32) 0x00000000, (uint8) 2 }, // Filter 1 
    [...] 

用大括號每個初始化初始化數組的不同元素。您的問題{(uint32) 0x01FFFFFF}, {(uint8) 2},的第一個表格最終會生成陣列的兩個成員(每個成員的a設置爲給定值,並且b設置爲零)。您可以通過添加約束的明確陣列(這將導致編譯器抱怨過多的初始化從你的問題看這個你的榜樣

第二種形式沒有你它到底要什麼:當沒有任何括號並且數組元素是一個結構體,這些值被賦值給連續的字段。這裏有一個可選的警告(可能是-Wall或類似的東西),因爲這個答案給出的表單使你的意圖更清晰一些。