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
};
4

3 回答 3

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或类似的东西),因为这个答案中给出的表格让你的意图更清楚了。

于 2011-09-05T01:50:26.310 回答
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
};
于 2011-09-05T01:45:34.410 回答
0

看起来您错过了{这一行开头的 a:

(uint32) 0x00000000}, {(uint8) 2}   // Filter 15
于 2011-09-05T01:21:38.357 回答