Wireshark-dev: Re: [Wireshark-dev] initializing a variable with a non-constant value
On Oct 14, 2012, at 10:36 AM, Martin Kaiser <lists@xxxxxxxxx> wrote:
> doc/README.developer says
>
> Don't initialize variables in their declaration with non-constant
> values. Not all compilers support this. E.g. don't use
> guint32 i = somearray[2];
Actually, that should only apply to variables with static storage duration (which includes both static and non-static variables) as opposed to automatic storage duration.
My copy of C90 (I gave away my dead-tree copy of ANSI C89 and couldn't find a digital version, just a digital version of C90) says, in "6.5.7 Initialization":
All the expressions in an initializer for an object that has static storage duration or in an initializer list for an object that has aggregate or union type shall be constant expressions.
My copy of C99 says, in "6.7.8 Initialization":
All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.
so that still applies in C99.
I'm not sure which compilers and linkers, if any, support arbitrary expressions as initializers for objects with static storage duration.
I'm also not sure which compilers and linkers, if any, still *don't* support initialization of objects with *automatic* storage duration with arbitrary expressions - that was introduced in C89, so maybe some compilers that mostly but not completely supported C89 didn't allow it, and maybe we ran into that problem at one point, but I suspect all C compilers support it at this point as they at least should all support C89.