微控制器C:初始化元素不是常数

时间:2014-05-30 17:02:46

标签: c

const char menu_000[] = " [Power Supply]";          // 0
const char menu_001[] = "  -800 Module1";           // 1
const char menu_002[] = "  -1800 Module1";          // 2
const char menu_003[] = "  -800 Module2";           // 3
const char menu_004[] = "  -1800 Module2";          // 4
const char menu_005[] = "  -800 Module3";           // 5
const char menu_006[] = "  -1800 Module3";          // 6
const char menu_007[] = "  -800 Module4";           // 7
const char menu_008[] = "  -1800 Module4";          // 8


// Array of entries
MenuEntry menu[] =
{
    {menu_000, 9,  0,  0,  0,  0},      // 0
    {menu_001, 9,  1,  2,  1,   readmodule(1)},
    {menu_002, 9,  1,  3,  2,   readmodule(2)},
    {menu_003, 9,  2,  4,  3,   readmodule(3)},
    {menu_004, 9,  3,  5,  4,   readmodule(4)},
    {menu_005, 9,  4,  6,  5,   readmodule(5)},
    {menu_006, 9,  5,  7,  6,   readmodule(6)},
    {menu_007, 9,  6,  8,  7,   readmodule(7)},
    {menu_008, 9,  7,  8,  8,   readmodule(8)}
    };

我正在尝试构建此结构,即以这种方式定义:

    typedef const struct MenuStructure
{   const char *text;
    unsigned char num_menupoints;
    unsigned char up;
    unsigned char down;
    unsigned char enter;
    void ( *fp )( void );
    // unsigned char value;
}MenuEntry;

但我一直收到这个错误:"初始化元素不是常数" 我该怎么办?

1 个答案:

答案 0 :(得分:2)

void ( *fp )( void );是一个指向函数的指针,你必须分配一个没有参数的函数,这将编译:

#include <stdio.h>

void readmodule(void)
{
    /* do something */
}

const char menu_000[] = " [Power Supply]";          // 0
const char menu_001[] = "  -800 Module1";           // 1
const char menu_002[] = "  -1800 Module1";          // 2
const char menu_003[] = "  -800 Module2";           // 3
const char menu_004[] = "  -1800 Module2";          // 4
const char menu_005[] = "  -800 Module3";           // 5
const char menu_006[] = "  -1800 Module3";          // 6
const char menu_007[] = "  -800 Module4";           // 7
const char menu_008[] = "  -1800 Module4";          // 8

typedef const struct MenuStructure
{   const char *text;
    unsigned char num_menupoints;
    unsigned char up;
    unsigned char down;
    unsigned char enter;
    void ( *fp )( void );
} MenuEntry;

// Array of entries
MenuEntry menu[] =
{
    {menu_000, 9,  0,  0,  0,   0}, /* better to use NULL instead of 0 for fp */
    {menu_001, 9,  1,  2,  1,   readmodule},
    {menu_002, 9,  1,  3,  2,   readmodule},
    {menu_003, 9,  2,  4,  3,   readmodule},
    {menu_004, 9,  3,  5,  4,   readmodule},
    {menu_005, 9,  4,  6,  5,   readmodule},
    {menu_006, 9,  5,  7,  6,   readmodule},
    {menu_007, 9,  6,  8,  7,   readmodule},
    {menu_008, 9,  7,  8,  8,   readmodule}
};

int main(void)
{
    return 0;
}