初始化程序周围缺少大括号

时间:2011-09-05 01:18:22

标签: gcc mplab

编译器是MPLAB 8下的GCC,dsPIC33 ver 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
};

3 个答案:

答案 0 :(得分: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或类似的东西打开),因为这个答案中给出的表单使你的意图更清晰。

答案 1 :(得分:0)

看起来你错过了这一行开头的{

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

答案 2 :(得分:0)

在文件中可能是一个未闭合的支撑......我们需要在上下文中看到这个 - 放在它自己的文件中,你的第二个数组编译得很好。我使用find-and-replace更改了类型,这不会产生

的错误或警告
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
};