This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Error : non-address/-constant initializer

Why below line of code is not compiling successfully?
const unsigned char *a[]={
  "Hello",
   "Hi"
};

const unsigned char *b[]={
  a[0],
   a[1]
};

Parents
  • The initializer for the array b should consist of address constants. Here is what C90 says about them:

    An address constant is a pointer to an lvalue designating an object of static storage duration, or to a function designator. Address constants must be created explicitly by using the unary & operator, or implicitly by using an expression of array or function type. The array subscript [] and member access operators . and ->, the address & and indirection * unary operators, and pointer casts can be used to create an address constant, but the value of an object cannot be accessed by use of these operators.

    It seems that Keil followed the standard to the letter here. The standard explicitly disallows the use of [] to access an object in an initializer, which you are trying to do in your code.

Reply
  • The initializer for the array b should consist of address constants. Here is what C90 says about them:

    An address constant is a pointer to an lvalue designating an object of static storage duration, or to a function designator. Address constants must be created explicitly by using the unary & operator, or implicitly by using an expression of array or function type. The array subscript [] and member access operators . and ->, the address & and indirection * unary operators, and pointer casts can be used to create an address constant, but the value of an object cannot be accessed by use of these operators.

    It seems that Keil followed the standard to the letter here. The standard explicitly disallows the use of [] to access an object in an initializer, which you are trying to do in your code.

Children