You are here:

C/structure declaration in C

Advertisement


Question
Can I declare structure like this:
typedef struct a
{
       int i : 4;
       unsigned dirty : 1;
       char *name;

}b;

I tried it using a gcc compiler it does not give an error.

I even want to know what is the use of such type of declaration.

Answer
Yes. that works.
You are limiting the number of bits of i to 4 and dirty to 1.
So, even if you give a bigger number, they will truncate and only hold the last 4 or 1 digit.
This will be helpful, if you know the max limit and will help in conserving the memory.

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.