Re: predict the answer



sophia said:

On Mar 7, 12:29 pm, Richard Heathfield <r...@xxxxxxxxxxxxxxx> wrote:


arrays are guaranteed to be contiguous with no padding before or
after any array member

Is it because all the elements are of the same type ?

now consider this

int a[] = {10,20,30,40,50};
float f = 3.14f;
void *p[]= {&a[1],&a[0],&a[2],&a[4],&f};

now my question is

In all cases all the elements of p will be stored contiguous with no
padding before or after any array member ?

3.5.7 of C89: "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."

An array has aggregate type.

3.4: " More latitude is permitted for constant expressions in
initializers. Such a constant expression shall evaluate to one of the
following:

* an arithmetic constant expression,

* an address constant, or

* an address constant for an object type plus or minus an integral
constant expression.

[...] An address constant is a pointer to an lvalue designating an object
of static storage duration, or to a function designator".

So the initialisation of p[] is illegal (but does not violate a constraint,
so no diagnostic message is required, but the behaviour of the program is
undefined).

You could fix this by making a[] and f static.

Once you've done that, the answer is "yes" - p[] will be an array of five
void *, each of which occupies exactly sizeof(void *) bytes.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
.



Relevant Pages

  • Defect Report: String literals can not be used to initialize static compound literals?
    ... Compound literals, paragraph 3 in ISO/IEC 9899:1999 C Standard ... constant expression, can not be taken to initialize a compound literal ... Obviously string literals should be mentioned together with constant ... initializer list shall consist of constant expressions or string ...
    (comp.std.c)
  • Re: C89: Compile-time constant required for initialiser?
    ... Fair enough it's not a compile-time constant... ... they need to be initialised with compile time constants. ... a "constant expression" is called for. ... All the expressions in an initializer for an object that has static ...
    (comp.lang.c)
  • Re: initializer element is not constant
    ... error "initializer element is not constant" on the below ... The code compiles fine with other compilers I ... is not a constant expression in C. ... int main ...
    (comp.lang.c)
  • Re: Compile-time constant in C89?
    ... an easy way to test whether an initializer is a valid const expression ... >> is to just put the definition out at file scope and see if it compiles. ... This sent me to the Standard to study up on constant expressions, ... "constant expression" involving the difference of two pointers would fare. ...
    (comp.lang.c)
  • Re: Compile-time constant in C89?
    ... >This sent me to the Standard to study up on constant expressions, ... > * What's a constant expression? ... The operands of an integer constant expression must be integer ... OTOH, the initializer section ...
    (comp.lang.c)