niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » C
Warning comes when using Unions
Post new topic   Reply to topic Page 1 of 1 [14 Posts] View previous topic :: View next topic
Author Message
neutron*star
*nix forums Guru


Joined: 21 Feb 2005
Posts: 2039

PostPosted: Fri Jun 02, 2006 4:16 am    Post subject: Re: Warning comes when using Unions Reply with quote

Dann Corbit said:

<lots>

WB, Dann. LTNS. How's tricks?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Back to top
Dann Corbit
*nix forums Guru Wannabe


Joined: 09 Mar 2005
Posts: 149

PostPosted: Fri Jun 02, 2006 2:25 am    Post subject: Re: Warning comes when using Unions Reply with quote

"Kantha" <bj.srikanth@gmail.com> wrote in message
news:1148536025.629811.11290@i40g2000cwc.googlegroups.com...
Quote:
Hi all,
I have declared an Union as follows
typedef union
{

struct interrupt_bits
{

unsigned char c_int_hs_fs_status : 1,
c_setup_intflag : 1,
c_hs_fs_state : 1,
c_int_suspend : 1,
c_int_resume : 1,
c_bus_reset : 1,
c_int_setup : 1,
c_int_ep0tx : 1,
c_int_ep0rx : 1,
c_int_ep2tx : 1,
c_int_ep2rx : 1;
} BITS;
short s_interrupt_flags;
}USB;
USB Usb;

Now when I compile the program , I get the a warning message saying
that
" warning: nonstandard type for a bit field". I am using a Texas
Instruments Cross Compiler(Code Composer Studio). Can anybody suggest
how to eliminate the warining message ??
Thanks in advance,
Srikanth

A bit picky. The data type for bit fields should be:

"A bit-field shall have a type that is a qualified or unqualified version of
_Bool, signed int, unsigned int, or some other implementation-defined type."

The ANSI/ISO C standard says (more fully):

ŠISO/IEC ISO/IEC 9899:1999 (E)
Forward references: enumeration specifiers (6.7.2.2), structure and union
specifiers
(6.7.2.1), tags (6.7.2.3), type definitions (6.7.7).
6.7.2.1 Structure and union specifiers
Syntax
1 struct-or-union-specifier:
struct-or-union identifieropt { struct-declaration-list }
struct-or-union identifier
struct-or-union:
struct
union
struct-declaration-list:
struct-declaration
struct-declaration-list struct-declaration
struct-declaration:
specifier-qualifier-list struct-declarator-list ;
specifier-qualifier-list:
type-specifier specifier-qualifier-listopt
type-qualifier specifier-qualifier-listopt
struct-declarator-list:
struct-declarator
struct-declarator-list , struct-declarator
struct-declarator:
declarator
declaratoropt : constant-expression
Constraints
2 A structure or union shall not contain a member with incomplete or
function type (hence, a structure shall not contain an instance of itself,
but may contain a pointer to an instance of itself), except that the last
member of a structure with more than one named member may have incomplete
array type; such a structure (and any union containing, possibly
recursively, a member that is such a structure) shall not be a member of a
structure or an element of an array.
3 The expression that specifies the width of a bit-field shall be an integer
constant expression that has nonnegative value that shall not exceed the
number of bits in an object of the type that is specified if the colon and
expression are omitted. If the value is zero, the declaration shall have no
declarator.
4 A bit-field shall have a type that is a qualified or unqualified version
of _Bool, signed int, unsigned int, or some other implementation-defined
type.
Semantics
5 As discussed in 6.2.5, a structure is a type consisting of a sequence of
members, whose storage is allocated in an ordered sequence, and a union is a
type consisting of a sequence of members whose storage overlap.
6 Structure and union specifiers have the same form.
7 The presence of a struct-declaration-list in a struct-or-union-specifier
declares a new type, within a translation unit. The struct-declaration-list
is a sequence of declarations for the members of the structure or union. If
the struct-declaration-list contains no named members, the behavior is
undefined. The type is incomplete until after the } that terminates the
list.
8 A member of a structure or union may have any object type other than a
variably modified type.102) In addition, a member may be declared to consist
of a specified number of bits (including a sign bit, if any). Such a member
is called a bit-field;103) its width is preceded by a colon.
9 A bit-field is interpreted as a signed or unsigned integer type consisting
of the specified number of bits.104) If the value 0 or 1 is stored into a
nonzero-width bit-field of type _Bool, the value of the bit-field shall
compare equal to the value stored.
10 An implementation may allocate any addressable storage unit large enough
to hold a bitfield. If enough space remains, a bit-field that immediately
follows another bit-field in a structure shall be packed into adjacent bits
of the same unit. If insufficient space remains, whether a bit-field that
does not fit is put into the next unit or overlaps adjacent units is
implementation-defined. The order of allocation of bit-fields within a unit
(high-order to low-order or low-order to high-order) is
implementation-defined. The alignment of the addressable storage unit is
unspecified.
11 A bit-field declaration with no declarator, but only a colon and a width,
indicates an unnamed bit-field.105) As a special case, a bit-field structure
member with a width of 0 indicates that no further bit-field is to be packed
into the unit in which the previous bit-field, if any, was placed.
12 Each non-bit-field member of a structure or union object is aligned in an
implementationdefined manner appropriate to its type.
13 Within a structure object, the non-bit-field members and the units in
which bit-fields reside have addresses that increase in the order in which
they are declared. A pointer to a structure object, suitably converted,
points to its initial member (or if that member is a bit-field, then to the
unit in which it resides), and vice versa. There may be unnamed padding
within a structure object, but not at its beginning.
14 The size of a union is sufficient to contain the largest of its members.
The value of at most one of the members can be stored in a union object at
any time. A pointer to a union object, suitably converted, points to each of
its members (or if a member is a bitfield, then to the unit in which it
resides), and vice versa.
15 There may be unnamed padding at the end of a structure or union.
16 As a special case, the last element of a structure with more than one
named member may have an incomplete array type; this is called a flexible
array member. With two exceptions, the flexible array member is ignored.
First, the size of the structure shall be equal to the offset of the last
element of an otherwise identical structure that replaces the flexible array
member with an array of unspecified length.106) Second, when a . (or ->)
operator has a left operand that is (a pointer to) a structure with a
flexible array member and the right operand names that member, it behaves as
if that member were replaced with the longest array (with the same element
type) that would not make the structure larger than the object being
accessed; the offset of the array shall remain that of the flexible array
member, even if this would differ from that of the replacement array. If
this array would have no elements, it behaves as if it had one element but
the behavior is undefined if any attempt is made to access that element or
to generate a pointer one past it.
17 EXAMPLE Assuming that all array members are aligned the same, after the
declarations:
struct s { int n; double d[]; };
struct ss { int n; double d[1]; };
the three expressions:
sizeof (struct s)
offsetof(struct s, d)
offsetof(struct ss, d)
have the same value. The structure struct s has a flexible array member d.
18 If sizeof (double) is 8, then after the following code is executed:
struct s *s1;
struct s *s2;
s1 = malloc(sizeof (struct s) + 64);
s2 = malloc(sizeof (struct s) + 46);
and assuming that the calls to malloc succeed, the objects pointed to by s1
and s2 behave as if the
identifiers had been declared as:
struct { int n; double d[8]; } *s1;
struct { int n; double d[5]; } *s2;
19 Following the further successful assignments:
s1 = malloc(sizeof (struct s) + 10);
s2 = malloc(sizeof (struct s) + 6);
they then behave as if the declarations were:
struct { int n; double d[1]; } *s1, *s2;
and:
double *dp;
dp = &(s1->d[0]); // valid
*dp = 42; // valid
dp = &(s2->d[0]); // valid
*dp = 42; // undefined behavior
20 The assignment:
*s1 = *s2;
only copies the member n and not any of the array elements. Similarly:
struct s t1 = { 0 }; // valid
struct s t2 = { 2 }; // valid
struct ss tt = { 1, { 4.2 }}; // valid
struct s t3 = { 1, { 4.2 }}; // invalid: there is nothing for the 4.2 to
initialize
t1.n = 4; // valid
t1.d[0] = 4.2; // undefined behavior
Forward references: tags (6.7.2.3).

102) A structure or union can not contain a member with a variably modified
type because member names are not ordinary identifiers as defined in 6.2.3.
103) The unary & (address-of) operator cannot be applied to a bit-field
object; thus, there are no pointers to or arrays of bit-field objects.
104) As specified in 6.7.2 above, if the actual type specifier used is int
or a typedef-name defined as int, then it is implementation-defined whether
the bit-field is signed or unsigned.
105) An unnamed bit-field structure member is useful for padding to conform
to externally imposed layouts.
106) The length is unspecified to allow for the fact that implementations
may give array members different alignments according to their lengths.
Back to top
J. J. Farrell
*nix forums addict


Joined: 09 Mar 2005
Posts: 57

PostPosted: Thu May 25, 2006 11:59 pm    Post subject: Re: Warning comes when using Unions Reply with quote

Srikanth wrote:
Quote:

Instead of declaring the structue elements as unsigned char ., I
declared it as a bool type(courtesy :
http://www.splint.org/guide/bool.c.html) and included <bool.h>.
I could able to restrict the earlier warnings.
Now my union looks like this
typedef union
{
struct interrupt_bits
{

bool c_int_hs_fs_status : 1,
c_setup_intflag : 1,
c_hs_fs_state : 1,
} BITS;
short s_interrupt_flags;
}USB;
I have pasted the bool.h file below . But still I donno how does this
work ?? Any idea??

...
typedef int bool;
...

Did you read the message to which you replied? It said:

Quote:
C99 para 6.7.2.1 says "A bit-field shall have a type that is
a qualified or unqualified version of _Bool, signed int,
unsigned int, or some other implementation-defined type".
I think earlier versions of the language were more restrictive
on this.

You've change the type to bool, which your header typedefs to an int.
Your definition now uses one of the allowed types for a bit-field, so
your code is now "legal" C, so the compiler has stopped complaining.
Back to top
Keith Thompson
*nix forums Guru


Joined: 28 Feb 2005
Posts: 5173

PostPosted: Thu May 25, 2006 6:40 pm    Post subject: Re: Warning comes when using Unions Reply with quote

"Srikanth" <bj.srikanth@gmail.com> writes:
Quote:
I am sorry if it did confuse you all !!! Now I think the problem wont
repeat as I have updated my profile . Anyways thanks a lot for your
valuable suggestion.
Regards,
Srikanth

Read <http://cfaj.freeshell.org/google/>. Read it now. Follow its
advice before you post another followup here.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Back to top
CBFalconer
*nix forums Guru


Joined: 09 Mar 2005
Posts: 2502

PostPosted: Thu May 25, 2006 3:07 pm    Post subject: Re: Warning comes when using Unions Reply with quote

santosh wrote:
Quote:
Srikanth wrote:

Instead of declaring the structue elements as unsigned char ...
... snip ...
Are you the OP? If so, why are you posting under multiple pseudonyms?
It will make it harder for readers to reply to you. Try to post under
any one name and quote the post you're replying to. Not all Usenet
readers can access previous posts. For how to do it under Google Groups
read the following:

http://cfaj.freeshell.org/google/
http://clc-wiki.net/wiki/Introduction_to_comp.lang.c
http://www.safalra.com/special/googlegroupsreply/

IIRC he has refused for a long time to quote properly, and as a
result has been plonked here.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
Back to top
William J. Leary Jr.
*nix forums beginner


Joined: 15 Oct 2005
Posts: 35

PostPosted: Thu May 25, 2006 11:19 am    Post subject: Re: Warning comes when using Unions Reply with quote

"Keith Thompson" <kst-u@mib.org> wrote in message
news:lnr72iczbn.fsf@nuthaus.mib.org...
Quote:
"William J. Leary Jr." <Bill_Leary@msn.com> writes:
Don't you still need to declare the data type for each element?

That is:
unsigned char c_int_hs_fs_status : 1,
unsigned char c_setup_intflag : 1,
and so on.

When I try it your way, I get a "syntax error: identifier" on the
c_setup_intflag:1 line and various other errors on the remainder.
When I add data types before each one, no errors.

Did you copy-and-paste the eact code that was posted?

Just the structure.

Quote:
It compiled
cleanly for me (though I got warnings when I told the compiler to be
more picky).

I didn't save the code from last night, but I've just tried what I think I did
then again and get no errors.

Quote:
I do prefer to declare each member separately, but declaring them
joined by commas is perfectly legal. Similarly, this:
int x, y;
is equivalent to this:
int x;
int y;

His approach is the one I normally use for embedded work, where the compiler
has declared rules guaranteeing how it processes bit fields into the various
data types. It would have declared an error in his example because there were
eleven bits being declared into an eight bit space. That is, it doesn't roll
over to the next byte automatically and if you declare fewer than the number of
bits for a data type the remainder are unused. Yes, I know it's non-standards,
but it's useful for embedded work, especially to map a structure onto hardware.

I guess I'd been using that so long that I was thinking that the comma format
was a non-standard extension.

But...

Now I wish I'd saved the code from last night so I could see what I did then.
I must have screwed up the paste somewhow, though it's hard to see in what way
a paste error could have caused that compiler error.

- Bill
Back to top
Kantha
*nix forums beginner


Joined: 25 May 2006
Posts: 5

PostPosted: Thu May 25, 2006 10:25 am    Post subject: Re: Warning comes when using Unions Reply with quote

I am sorry if it did confuse you all !!! Now I think the problem wont
repeat as I have updated my profile . Anyways thanks a lot for your
valuable suggestion.
Regards,
Srikanth
Back to top
santosh
*nix forums Guru


Joined: 23 Feb 2005
Posts: 351

PostPosted: Thu May 25, 2006 9:16 am    Post subject: Re: Warning comes when using Unions Reply with quote

Srikanth wrote:
Quote:
Hi all,
Instead of declaring the structue elements as unsigned char ...
.... snip ...

Are you the OP? If so, why are you posting under multiple pseudonyms?
It will make it harder for readers to reply to you. Try to post under
any one name and quote the post you're replying to. Not all Usenet
readers can access previous posts. For how to do it under Google Groups
read the following:

<http://cfaj.freeshell.org/google/>
<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
<http://www.safalra.com/special/googlegroupsreply/>
Back to top
Kantha
*nix forums beginner


Joined: 25 May 2006
Posts: 5

PostPosted: Thu May 25, 2006 9:04 am    Post subject: Re: Warning comes when using Unions Reply with quote

Hi all,
Instead of declaring the structue elements as unsigned char ., I
declared it as a bool type(courtesy :
http://www.splint.org/guide/bool.c.html) and included <bool.h>.
I could able to restrict the earlier warnings.
Now my union looks like this
typedef union
{
struct interrupt_bits
{

bool c_int_hs_fs_status : 1,
c_setup_intflag : 1,
c_hs_fs_state : 1,
} BITS;
short s_interrupt_flags;
}USB;
I have pasted the bool.h file below . But still I donno how does this
work ?? Any idea??
/* bool.h */
#ifndef BOOL_H
#define BOOL_H

#ifndef FALSE
#define FALSE 0
#endif

#ifndef TRUE
#define TRUE (! FALSE)
#endif

typedef int bool;

# define bool_initMod()
# define bool_unparse(b) ((b) ? "true" : "false" )
# define bool_not(b) ((b) ? FALSE : TRUE)
# define bool_equal(a,b) ((a) ? (b) : !(b))

# endif

Thanks in advance,
Srikanth
Back to top
Keith Thompson
*nix forums Guru


Joined: 28 Feb 2005
Posts: 5173

PostPosted: Thu May 25, 2006 7:56 am    Post subject: Re: Warning comes when using Unions Reply with quote

"William J. Leary Jr." <Bill_Leary@msn.com> writes:
Quote:
"Kantha" <bj.srikanth@gmail.com> wrote in message
news:1148536025.629811.11290@i40g2000cwc.googlegroups.com...
Hi all,
I have declared an Union as follows
typedef union
{

struct interrupt_bits
{

unsigned char c_int_hs_fs_status : 1,
c_setup_intflag : 1,

Don't you still need to declare the data type for each element?

That is:
unsigned char c_int_hs_fs_status : 1,
unsigned char c_setup_intflag : 1,
and so on.

When I try it your way, I get a "syntax error: identifier" on the
c_setup_intflag:1 line and various other errors on the remainder.
When I add data types before each one, no errors.

Did you copy-and-paste the eact code that was posted? It compiled
cleanly for me (though I got warnings when I told the compiler to be
more picky).

I do prefer to declare each member separately, but declaring them
joined by commas is perfectly legal. Similarly, this:
int x, y;
is equivalent to this:
int x;
int y;

The real problem is that int, signed int, unsigned int, and _Bool (C99
only) are the only portably legal types for bit fields. (Plain int is
equivalent to either signed int or unsigned int, depending on the
implementation. This is true only for bit fields; in all other
contexts, plain int is signed int.)

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Back to top
J. J. Farrell
*nix forums addict


Joined: 09 Mar 2005
Posts: 57

PostPosted: Thu May 25, 2006 7:03 am    Post subject: Re: Warning comes when using Unions Reply with quote

Kantha wrote:
Quote:
Hi all,
I have declared an Union as follows
typedef union
{

struct interrupt_bits
{

unsigned char c_int_hs_fs_status : 1,
c_setup_intflag : 1,
c_hs_fs_state : 1,
c_int_suspend : 1,
c_int_resume : 1,
c_bus_reset : 1,
c_int_setup : 1,
c_int_ep0tx : 1,
c_int_ep0rx : 1,
c_int_ep2tx : 1,
c_int_ep2rx : 1;
} BITS;
short s_interrupt_flags;
}USB;
USB Usb;

Now when I compile the program , I get the a warning message saying
that
" warning: nonstandard type for a bit field". I am using a Texas
Instruments Cross Compiler(Code Composer Studio). Can anybody suggest
how to eliminate the warining message ??
Thanks in advance,
Srikanth

C99 para 6.7.2.1 says "A bit-field shall have a type that is a
qualified or unqualified version of _Bool, signed int, unsigned int, or
some other implementation-defined type". I think earlier versions of
the language were more restrictive on this.
Back to top
Kantha
*nix forums beginner


Joined: 25 May 2006
Posts: 5

PostPosted: Thu May 25, 2006 6:50 am    Post subject: Re: Warning comes when using Unions Reply with quote

I dont see a reason why is it giving error..,You can do it in both
ways.., its basically alloting a single bit to each element in a
structure.
The problem i am facing is that although the program works fine Smile, I
am not able to eliminate the warning..Sad
Back to top
William J. Leary Jr.
*nix forums beginner


Joined: 15 Oct 2005
Posts: 35

PostPosted: Thu May 25, 2006 6:08 am    Post subject: Re: Warning comes when using Unions Reply with quote

"Kantha" <bj.srikanth@gmail.com> wrote in message
news:1148536025.629811.11290@i40g2000cwc.googlegroups.com...
Quote:
Hi all,
I have declared an Union as follows
typedef union
{

struct interrupt_bits
{

unsigned char c_int_hs_fs_status : 1,
c_setup_intflag : 1,

Don't you still need to declare the data type for each element?

That is:
unsigned char c_int_hs_fs_status : 1,
unsigned char c_setup_intflag : 1,
and so on.

When I try it your way, I get a "syntax error: identifier" on the
c_setup_intflag:1 line and various other errors on the remainder. When I add
data types before each one, no errors.

- Bill
Back to top
Kantha
*nix forums beginner


Joined: 25 May 2006
Posts: 5

PostPosted: Thu May 25, 2006 5:47 am    Post subject: Warning comes when using Unions Reply with quote

Hi all,
I have declared an Union as follows
typedef union
{

struct interrupt_bits
{

unsigned char c_int_hs_fs_status : 1,
c_setup_intflag : 1,
c_hs_fs_state : 1,
c_int_suspend : 1,
c_int_resume : 1,
c_bus_reset : 1,
c_int_setup : 1,
c_int_ep0tx : 1,
c_int_ep0rx : 1,
c_int_ep2tx : 1,
c_int_ep2rx : 1;
} BITS;
short s_interrupt_flags;
}USB;
USB Usb;

Now when I compile the program , I get the a warning message saying
that
" warning: nonstandard type for a bit field". I am using a Texas
Instruments Cross Compiler(Code Composer Studio). Can anybody suggest
how to eliminate the warining message ??
Thanks in advance,
Srikanth
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [14 Posts] View previous topic :: View next topic
The time now is Wed Dec 03, 2008 9:31 pm | All times are GMT
navigation Forum index » Programming » C
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Tagged unions johan.tibell@gmail.com C 1 Fri Jul 21, 2006 1:00 pm
No new posts Warning when new attributes are added to classes at run time Matthew Wilson python 7 Wed Jul 19, 2006 8:42 pm
No new posts Query regarding warning removal when using extern gaurav.dube@gmail.com C 4 Tue Jul 18, 2006 5:34 pm
No new posts WARNING - current temperature (60.0C) exceeds safe limits renchao85@gmail.com FreeBSD 1 Tue Jul 18, 2006 12:23 am
No new posts WARNING: Hard link count is wrong for /selinux amit Setup 8 Mon Jul 17, 2006 7:21 pm

Mortgages | Free Ringtones | Credit Cards | Loans | Adverse Credit Remortgage
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2975s ][ Queries: 20 (0.1395s) ][ GZIP on - Debug on ]