473,405 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Difference between Unions and Structures...

Hi,

I want know the differences between Unions and Structures in C
programming.

Thank you..
Nov 3 '08 #1
6 3662
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Ravikiran wrote:
I want know the differences between Unions and Structures in C
programming.
The size of union is the size of its largest member. In most
implementations (but standard does *not* require it) all members of
union are placed at the same location in memory.
Structures are just "normal" groups of variables.

Pawel Dziepak

PS I really shouldn't help you that way, there is a lot of information
about structures and unions in many FAQs, articles, etc.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iEYEARECAAYFAkkO3r4ACgkQPFW+cUiIHNp5SwCgsATEiHUwMy Ad4dGgVJQQAUR2
MCoAn1+6G5wqv5h6JBxjP3x++K5SDBLT
=z5G+
-----END PGP SIGNATURE-----
Nov 3 '08 #2
On Mon, 03 Nov 2008 12:21:34 +0100,
Pawel Dziepak <pd******@quarnos.orgwrote:
>
Ravikiran wrote:
>I want know the differences between Unions and Structures in C
programming.

The size of union is the size of its largest member. In most
implementations (but standard does *not* require it) all members of
union are placed at the same location in memory.
From 6.2.5 - 20

— A union type describes an overlapping nonempty set of member
objects, each of which has an optionally specified name and possibly
distinct type.

Also, from 6.5.8 - 5

All pointers to members of the same union object compare equal.

I'd say that pretty much gurantees that the members of a union, as you
say it, are placed at the same location in memory.

Martien

PS. quotes from n1256. Wording in actual c99 standard is identical. I
don't own a c89 copy, but in a draft for that standard, similar ior
identical wording for the above quoted is present

--
|
Martien Verbruggen | Since light travels faster than sound, is
| that why some people appear bright until you
| hear them speak?
Nov 3 '08 #3
gk
On Nov 3, 6:06*pm, Martien Verbruggen <m...@heliotrope.com.auwrote:
On Mon, 03 Nov 2008 12:21:34 +0100,
* * * * Pawel Dziepak <pdzie...@quarnos.orgwrote:
Ravikiran wrote:
I want know the differences between Unions and Structures in C
programming.
The size of union is the size of its largest member. In most
implementations (but standard does *not* require it) all members of
union are placed at the same location in memory.

From 6.2.5 - 20

* — A union type describes an overlapping nonempty set of member
* * objects, each of which has an optionally specified name and possibly
* * distinct type.

Also, from 6.5.8 - 5

* All pointers to members of the same union object compare equal.

I'd say that pretty much gurantees that the members of a union, as you
say it, are placed at the same location in memory.

Martien

PS. quotes from n1256. Wording in actual c99 standard is identical. I
don't own a c89 copy, but in a draft for that standard, similar ior
identical wording for the above quoted is present

--
* * * * * * * * * * * * |
Martien Verbruggen * * *| Since light travels faster than sound, is
* * * * * * * * * * * * | that why some people appear bright until you
* * * * * * * * * * * * | hear them speak?
the difference basically arises in the memory allocation.....while
structure allocates memory separately to each of its members, the same
is not the case with unions..it allocates memory only to the largest
available member...the rest of them are allocated within this block of
memory only
Nov 3 '08 #4
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Martien Verbruggen wrote:
I'd say that pretty much gurantees that the members of a union, as you
say it, are placed at the same location in memory.
Sorry, it was my fault. I heard that the location can be different, but
I didn't check that. Anyway, sorry for providing incorrect information.

Pawel Dziepak
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iEYEARECAAYFAkkPB1cACgkQPFW+cUiIHNpcSgCeMdASw61Y1o IZvce1+NR12WXA
xcsAn0jYzPyCUAKQsZmOnjbQydXs0M2P
=xpZ8
-----END PGP SIGNATURE-----
Nov 3 '08 #5
Ravikiran <ra***********@gmail.comwrites:
I want know the differences between Unions and Structures in C
programming.
If your textbook doesn't explain this clearly, you need a new
textbook. I recommend K&R2 (Kernighan & Ritchie, The C Programming
Language, 2nd Edition).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 3 '08 #6
Ravikiran wrote:
>
I want know the differences between Unions and Structures in C
programming.
Try the C standard. For example:

6.7.2.1 Structure and union specifiers

...

Semantics

[#4] As discussed in 6.2.5, a structure is a type consisting
of a sequence of members, whose storage is allocated in an
ordered sequence, and a union is a type consisting of a
sequence of members whose storage overlap.

and a good deal more.

Some useful references about C:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://c-faq.com/ (C-faq)
<http://benpfaff.org/writings/clc/off-topic.html>
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf(C99)
<http://cbfalconer.home.att.net/download/n869_txt.bz2 (pre-C99)
<http://www.dinkumware.com/c99.aspx (C-library}
<http://gcc.gnu.org/onlinedocs/ (GNU docs)
<http://clc-wiki.net/wiki/C_community:comp.lang.c:Introduction>

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Nov 3 '08 #7

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

Similar topics

11
by: Michael | last post by:
Hi guys I've been programming C++ for quite some time, but I have never used unions. I am trying to find a good exmaple of when they might be used. can anyone provide one?? Regards Michael.
6
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
9
by: Neil Zanella | last post by:
Hello, Some programmers like to use a coding convention where all names of variables that are pointers start with the letter p (and sometimes even use similar conventions for strings and other...
23
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...
2
by: minorguy | last post by:
1.) In C++/CLI, I have the following structures defined, which work as unions: public ref struct One { Int32 a; };
1
by: aarklon | last post by:
Hi folks, Recently i was reading the book C an advanced introduction by narain gehani, in page no:236 it is written as If a union contains several structures with a common initial sequence,...
3
by: Hallvard B Furuseth | last post by:
I'm getting horribly lost in the strict aliasing rules. Is this code correct? struct A { int x; }; struct B { int x, y; }; int foo( struct A *a ) { struct B *b = (struct B *) a; return b->x...
67
by: bluejack | last post by:
A recent post asking for help with unions reminded me of this component of the C language that I have only used a couple of times, and those almost entirely out of personal whim -- Unions for the...
2
by: Ravikiran | last post by:
Hi, I want know the differences between Unions and Structures in C programming. Thank you..
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.