Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

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



Application Security Testing: An Integral Part of DevOps

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.



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