Thursday, 29 June 2017

bit fields in structures

To avoid wastage of memory in structures, a group of bits can be packed together into an integer and its called a bit field. 

struct student;
{
 char name[30];
 unsigned sex:1;
 unsigned age:5;
 unsigned rollno:7;
 unsigned branch:2;
};
There are some limitations with respect to these bit fields, however:
1. Cannot scanf() directly into bit fields.
2. Pointers cannot be used on bit fields to access them.
3. Cannot have an array of bit fields. 
 
Will give compilation error on below. 
 
struct student a[100];
scanf("%d", &sex);
a[i].sex=sex; 
 
 
 
 

No comments:

Post a Comment