473,795 Members | 2,924 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question on bitfields & structs

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(st ruct struct1));
/* Prints 4 */
printf ("Size of struct 2 = %d\n",sizeof(st ruct 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

Jul 20 '06 #1
1 1364
sarathy wrote:
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(st ruct struct1));
/* Prints 4 */
printf ("Size of struct 2 = %d\n",sizeof(st ruct 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?
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Jul 20 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3019
by: Capstar | last post by:
Hi NG, Does the following code invoke undefined behaviour? I compiled it and it runs fine. I think this is fine because I think the last 3 arguments of struct a are organised in memory the same way as the 3 arguments of struct b. But if my previous statement isn't true in all cases or on all platforms or with all compilers, this probably will invoke undefined behaviour. Can anyone tell me if I'm right or not?
8
1909
by: Régis Troadec | last post by:
Hi all, I follow c.l.c. for only a short time and I would like to know why there isn't anything concerning bitfields among the FAQs. Is it because ... 1. of portability issues? 2. bitfields aren't enough useful to be discussed? 3. of the low frequency of questions concerning this topic? 4. of anything else?...
23
2838
by: rohit | last post by:
Hi, In my couple of years of experience, I have never found a single instance where I needed to use unions and bitfields(though I have used structures).I was just imagining where would these find relevance.Though both of these(bitfields and unions) are used where space is a constraint(so I can assume always in embedded systems,where memory is particularly less)and we want to save space/memory. As far as I have read, there is no...
6
2685
by: GalenTX | last post by:
I am looking for opinions on a possible approach to coping with bitfields in a heterogenous environment. We've got a bunch of legacy code which maps to hardware using bitfields. The code was developed for a PowerPC/VxWorks/Tornado enviromant, with the bits packed in a big-endian manner: msb first. We now have to develop an Intel/Windows/MSVC++ version (lsb assigned first) which must maintain compatibility with the hardware interface...
7
5535
by: Kevin | last post by:
Hi al I have an interesting question.... I am working witha Win API this is the Function Public Declare Function EnumJobs Lib "winspool.drv" Alias "EnumJobsA" (ByVal hPrinter As Long, ByVal FirstJob As Long, ByVal NoJobs As Long, ByVal Level As Long, pJob As Byte, ByVal cdBuf As Long, pcbNeeded As Long, pcReturned As Long) As Lon Which I got from the API viewer that comes with VB 6. I have tried to convert it to the following ...
13
2319
by: HARDCORECODER | last post by:
ok here is the question. I want to exract the first 4 bits in a int so let say int b = somenumber; int result= 0; result = b << 4; if I got this right result should contain the 4 bits that were shifter to the left right? :) and if I'm wrong how can i get an x number of bits into a variable?
64
3903
by: shaanxxx | last post by:
I have code which says #define MYBOOL int This code is very old. people who have written is not avaible. I was thinking what could be reason. 1) bool datatype was not available that time (10 years back: correct me if i am wrong) 2) int was word aligned (16bit and 32bit). Writter of above code doesnt want to give choice to compiler.
9
19260
by: cman | last post by:
Who can explain to me what bitfields are and how to use them in practice? I would need a fairly detailed explaination, I would be a newbie to advanced C programming features. What are the advantages of using bitfields? cman
3
2302
by: ajcrm125 | last post by:
I'm trying to utilize bitfields to make my file easier. But for some reason, I can't assign a value to this structure. I'm sure I must be missing something: typedef unsigned short WORD; WORD data; union uVCTR { WORD Ypos : 10; // Y position WORD Ysign : 1; // Y position sign bit
20
3429
by: Plissken.s | last post by:
Is there an efficient way to create a struct of flag in C++? I need to create a struct of boolean flag, like this: struct testStruct { bool flag1; bool flag2; bool flag3; bool flag4; bool flag5; bool flag6;
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9043
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7541
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.