0

I need to initialize a char array with macros as it's values. I've tried to do it like this:

    char    text[][255] = {
     "",
    /*  1 */        MACRO_("Foobar","Some text to translate"),
    /*  2 */        MACRO_("Foobar","Some more text to translate"),
//...
};

But I get this error:

error: initializer element is not constant

I think the compiler can't resolve the macro. Is there a way to make this work?

3
  • 5
    What is the macro definition? Commented Feb 20, 2013 at 12:13
  • What is the definition of MACRO_ ? Commented Feb 20, 2013 at 12:14
  • MACRO_ calls a function which translates the text. It's not my solution but I have to work with it :( Commented Feb 20, 2013 at 12:22

2 Answers 2

2

It is more likely that MACRO() is expanding to something non-constant, such as a function call to look up a translation. That's at least what is usually done in situations like those.

With GNU gettext, which also uses macros to mark and look upstrings for translation, you use a separate marking-only macro for situations like these (typically called N_()), then you pass the string to the run-time macro _() before using it.

You can't initialize an array with data that requires a function call in order to be computed, it must be constant data.

Sign up to request clarification or add additional context in comments.

2 Comments

I already know that I can't, but I still need to have this array. Any suggestions on how to do this?
@user2091046 the classic workaround is to put untranslated text in the array, and call the translation macro where the array is used.
0

> MACRO_ calls a function which translates the text. It's not my solution but I have to work with it

The fact is that you can not initialize your array at compile time with data coming from MACRO_ since it calls a function. However all is not lost. You can simply do this during run time

ex:

#define MACRO_ ...//whatever it does

int main()
{
   char    text[10][255] = {0}; // you'll have to set that first value

   strcpy(text[0],  MACRO_("Foobar","Some text to translate"));
   strcpy(text[1],  MACRO_("Foobar","Some more text to translate"));
   ...

Depending on your code, and what MACRO_ does, and what you're passing in, you might be able to do this in a loop which will save you some writing, but that will solve your problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.