Home > Mobile >  Pointer to constant array
Pointer to constant array

Time:07-18

I am making a multi-language interface for an AVR system, the strings are stored in the program memory with each language string placed in its own array. The idea is that when the user switches language, the pointer that contains the address of the currently selected language array will change. I have difficulties understanding the correct type of pointer I should use to point to these arrays. With the current code, I am getting a warning of "assignment discards 'const' qualifier from pointer target type".

Here is the code:

Languages.c:

const MEMORY_PREFIX wchar_t* const  Greek_text[] =
{
    [INITIAL_FEED_TEXT] = (const MEMORY_PREFIX wchar_t[]) {L"γεμισμα"},
    [NORMAL_BURN_TEXT] = (const MEMORY_PREFIX wchar_t[]) {L"Πληρης Καυση"},
}

const MEMORY_PREFIX wchar_t* const  English_text[] =
{
    [INITIAL_FEED_TEXT] = (const MEMORY_PREFIX wchar_t[]) {L"Initial Feed"},
    [NORMAL_BURN_TEXT] = (const MEMORY_PREFIX wchar_t[]) {L"Normal Burn"},
}

Languages.h:

#define MEMORY_PREFIX __flash
extern  const MEMORY_PREFIX wchar_t* const  Greek_text[];
extern  const MEMORY_PREFIX wchar_t* const  English_text[];

typedef enum lang_pt
{
    INITIAL_FEED_TEXT,
    NORMAL_BURN_TEXT
}lang_pt_t;

Menu.h:

typedef enum Language
{
 English,
 Greek,
}Language_type_t;

void Menu_select_language(void);

Menu.c:

static const MEMORY_PREFIX wchar_t** Unicode_text = Greek_text;

void Menu_select_language(Language_type_t language)
{
    /*Assign a language table to the language table pointer*/
    switch (language)
    {
    case English:
        Unicode_text = English_text;
        break;
    case Greek:
        Unicode_text =Greek_text;
        break;
    default:
        Unicode_text = English_text;
        break;
    }
}

CodePudding user response:

The number of the qualifier const in the declaration of a pointer

static const MEMORY_PREFIX wchar_t** Unicode_text = Greek_text;

does not corresponds to the number of the qualifier in the arrays.

You should write

static const MEMORY_PREFIX wchar_t * const * Unicode_text = Greek_text;

CodePudding user response:

You should add additional const qualifier:

static const MEMORY_PREFIX wchar_t* const * Unicode_text = Greek_text;

because not only the strings are constants, but the arrays of strings are constants too.

  • Related