Bitfield casting
- From: "arne" <arne.wiebalck@xxxxxxxxx>
- Date: 6 Mar 2007 12:07:09 -0800
Hi all,
cleaning up some elderly code, I stumbled across the following:
/**************************************************/
struct {
uint bf:8;
char a1[8];
char a2[8];
} binfo;
/* prototype added later */
int foo( char * );
int main( void )
{
foo( &binfo );
return 0;
}
int foo( char *c ) {
/*
** do something with c here ...
*/
return 0;
}
/**************************************************/
Note: The address of the bitfield bf of the struct binfo is used as
the
char* arg in foo().
The compiler was complaining, since there was no prototype for foo().
After adding it, the compiler was complaining, since the call of foo()
did
not match the prototype (which I can understand, since &binfo is
certainly
not exactly of type char*).
None the less, I have some questions:
1. Does it change the semantic of the code in any circumstances
(little
vs big endian, 64 bit architectures, ...) if I cast the argument of
foo like
foo( (char *)&binfo );
?
If the explicit cast is omitted, isn't the same cast done implicitely
anyway? If
yes: also if there is no prototype or what happens then?
2. Is &binfo _always_ pointing to the 8 bit wide bitfield bf (more
general:
are the addresses of a struct and of its first member always
identical)?
3. Are the addresses of members in a struct always ascending in the
same
order as the members have been defined? Or is the compiler allowed to
do some reordering (not padding)?
Thanks in advance for your responses!
Arne
.
- Follow-Ups:
- Re: Bitfield casting
- From: Keith Thompson
- Re: Bitfield casting
- From: Eric Sosman
- Re: Bitfield casting
- Prev by Date: Re: C Strings
- Next by Date: Re: Finding 1000 largest numbers from a file having some billion numbers
- Previous by thread: Re: Script or Program to check & view what's cached on the local news server ?
- Next by thread: Re: Bitfield casting
- Index(es):
Relevant Pages
|