devxlogo

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

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  Five Early Architecture Decisions That Quietly Get Expensive

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.