3
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)}
    };

I`m trying to build this structure, that is define this way:

    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;

but I keep getting this error: "initializer element is not constant" What should I do?

paulorfmmb
  • 43
  • 3
  • but I tried no declaring as a constant and still shows the same error – paulorfmmb May 30 '14 at 17:11
  • What does readmodule(1) do? does it return a function pointer? – cup May 30 '14 at 17:13
  • You should show us what `readmodule()` is. If that's a function then the return value can only be calculated at run-time. To find the non-constant I usually replace one per try all initializers by `0` until the error vanishes. This can help to identify the trouble-maker. – harper May 30 '14 at 17:14

1 Answers1

2

void ( *fp )( void ); is a pointer to function, you must assign a function without parameters, this will compile:

#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;
}
David Ranieri
  • 39,972
  • 7
  • 52
  • 94