Re: predict the answer
- From: Richard Heathfield <rjh@xxxxxxxxxxxxxxx>
- Date: Fri, 07 Mar 2008 15:43:28 +0000
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
.
- References:
- predict the answer
- From: sophia
- Re: predict the answer
- From: Richard Heathfield
- Re: predict the answer
- From: sophia
- predict the answer
- Prev by Date: Re: How printf() works???????
- Next by Date: Re: system function not executed correctly
- Previous by thread: Re: predict the answer
- Next by thread: Re: predict the answer
- Index(es):
Relevant Pages
|