Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Intermediate
Dec 10, 1999

Initializing a Bit Struct

To initialize a struct that contains bit fields, simply use the ={0}partial initialization list:

 
int main()
{
  struct MP3_HEADER 
  {  
     unsigned Sync:11;
     unsigned Version:2;
     unsigned Layer:2;
     unsigned Protection:1;
     unsigned Bitrate:4;
     unsigned Frequency:2;
     unsigned Padding:1;
     unsigned Private:1;
     unsigned ChannelMode:2;
     unsigned ModeExtension:2;
     unsigned Copyright:1;
     unsigned Original:1;
     unsigned Emphasis:2;
   };
  // create an instance and initialize it
  MP3_HEADER header  = {0}; /*set all members to zero*/
}
When you use a {0} partial initialization list, both C and C++ guarantee that all the members in the struct
Danny Kalev
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date