We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hello, I try compile this code (.c) with --c99 option struct sx { char c; char a[]; }; static struct sx xx = { 1, {[5] = 0} }; and have error error: #146: too many initializer values
How to initialize array inside struct ?
ARM C/C++ Compiler, 4.1 [Build 791] [Standard]
Thanks
You are misunderstanding the flexible array feature of C99.
May be nut this code compiled under GCC.
Well, GCC has a history of non-trivial language extensions, so this shouldn't be surprising. To me the question is: what is the expected size of the array in the structure xx? I cannot see a satisfactory answer to this question, so to me it is natural that C99 (as per the text of the C99 standard) does not allocate any memory for the array and does not expect an initializer for the array.
xx element c = 1 element a = array of 5 bytes with 0 values
element a = array of 5 bytes with 0 values
I wonder where this comes from. Even if (as you seem to suggest) the compiler looks at the initializer and uses the highest mentioned array element for the array size, the size would be 6 bytes. Anyway, this is beyond standard C99.
Mike K is correct. This is probably C99 compliant and may work for you:
struct sx { char c; char *a; }; static struct sx xx = { 1, (char[5]){0}};
Yes - this works fine. Only one remark - in this i have a pointer to a but not a array. Of cource it helpful but i can't port gcc and armcc sources (or need make different codes for process a[]</a> and *a</a>. But you answer really help me. Thanks Marc Crandall
yup, porting becomes a lot harder when you deviate from standards.
I'm sure the pointer solution will work with gcc as well.
Yes - :) i already make contional compling. Thanks again Marc
P.S. i use armcc as final for own binaries (smallest code genaration). And gcc compatibility for other people (as opensource).