Question on bitfields & structs

S

sarathy

Hi,

1. I executed the following code. But could not deduce anything from
the results. Can anyone please help me out.

# include <stdio.h>



struct struct1{ const :16; volatile :4;};


struct struct2{ int :1; unsigned :1;
const :16; volatile :4;};



int main()
{
printf ("Size of struct 1 = %d\n",sizeof(struct struct1));
/* Prints 4 */
printf ("Size of struct 2 = %d\n",sizeof(struct struct2));
/* Prints 3 */
}

2. Also what is meant by "incomplete type" in C?
3. What does tag refer to in a struct/union declaration?

Regards,
Sarathy
 
F

Flash Gordon

sarathy said:
Hi,

1. I executed the following code. But could not deduce anything from
the results. Can anyone please help me out.

Please try to format your code moderately sensibly. What you have posted
is a complete mess.
# include <stdio.h>

struct struct1{ const :16; volatile :4;};

struct struct2{ int :1; unsigned :1;
const :16; volatile :4;};

int main()
{
printf ("Size of struct 1 = %d\n",sizeof(struct struct1));
/* Prints 4 */
printf ("Size of struct 2 = %d\n",sizeof(struct struct2));
/* Prints 3 */
}

In struct2 your compiler chose to insert some padding bits. I also
deduced that you probably have an 8 bit char type on your system and
that your compiler accepts structures without any named fields. I
further deduce that you don't know that sizeof does not return an int.
2. Also what is meant by "incomplete type" in C?

A type that is not complete.
3. What does tag refer to in a struct/union declaration?

It is its name, so it refers to the struct or union you are declaring.

Have you read the relevant sections of your C text book yet?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top