Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can't - in C. In C initializing of global and local static variables are designed such that the compiler can put the values statically into the executable. It can't handle non-constant expressions as initializers. And only in C99, you can use non-constant expression in aggregate initializers - not so in C89! </p> <p>In your case, since your array is an array containing characters, each element has to be an arithmetic constant expression. Look what it says about those</p> <blockquote> <p>An arithmetic constant expression shall have arithmetic type and shall only have operands that are integer constants, floating constants, enumeration constants, character constants, and sizeof expressions.</p> </blockquote> <p>Surely this is not satisfied by your initializer, which uses an operand of pointer type. Surely, the other way is to initialize your array using a string literal, as it explains too</p> <blockquote> <p>All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.</p> </blockquote> <p>All quotes are taken out of the C99 TC3 committee draft. So to conclude, what you want to do - using non-constant expression - can't be done with C. You have several options: </p> <ul> <li>Write your stuff multiple times - one time reversed, and the other time not reversed. </li> <li>Change the language - C++ can do that all.</li> <li>If you <em>really</em> want to do that stuff, use an array of <code>char const*</code> instead</li> </ul> <p>Here is what i mean by the last option</p> <pre><code>char const c[] = "ABCD"; char const *f[] = { &amp;c[0], &amp;c[1], &amp;c[2], &amp;c[3] }; char const *g[] = { &amp;c[3], &amp;c[2], &amp;c[1], &amp;c[0] }; </code></pre> <p>That works fine, as an address constant expression is used to initialize the pointers</p> <blockquote> <p>An address constant is a null pointer, a pointer to an lvalue designating an object of static storage duration, or a pointer to a function designator; it shall be created explicitly using the unary &amp; operator or an integer constant cast to pointer type, or implicitly by the use of an expression of array or function type. The array-subscript [] and member-access . and -> operators, the address &amp; and indirection * unary operators, and pointer casts may be used in the creation of an address constant, but the value of an object shall not be accessed by use of these operators.</p> </blockquote> <p>You may have luck tweaking your compiler options - another quote:</p> <blockquote> <p>An implementation may accept other forms of constant expressions.</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload