1
const char* const back_slash = "\\";
const char* const open_braces ="[";

const char* const array[][2] = {

   {
     back_slash,
    open_braces,

    },
};

In this case i am getting

error: initializer element is not constant

Can you please help?

user2421350
  • 147
  • 1
  • 2
  • 8
  • 1
    Please have a look on answers given [Here](http://stackoverflow.com/questions/3025050/error-initializer-element-is-not-constant-when-trying-to-initialize-variable-w). – Dayal rai Mar 07 '14 at 07:23

4 Answers4

3

In section 6.7.8/4:

All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.

In the C language, the term "constant" refers to literal constants (like 1, 'a', "[" and so on). back_slash and open_braces are not compile-time constants.

Lundin
  • 195,001
  • 40
  • 254
  • 396
Sadique
  • 22,572
  • 7
  • 65
  • 91
0

An array initializer needs to be a comma-separated list of constant expressions; you are using variables.

0

These are const-qualified variables for C, so just variables and not constants in the context of initialization. Use defines to name such literals:

#define back_slash "\\"
#define open_braces "["
Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
0

You tried to be too pedantic: C doesn't have real compile time const declarations (with types). One way, to do get the const array part to compile is to declare backslash and open bracket (bracket '[', brace '{') with a #define, unless of course you need them somewhere else. Note about const declarations: one would expect that after 'const int i = 5', i could be used in the code freely and would not appear in the object file. It does, and this can bite you on a small embedded system.

user1666959
  • 1,805
  • 12
  • 11