C and C++ Programming Resources

Unions and Structures

Posted on October 16th, 2008 .

What is a Union?

If we are having the less memory to use in our program, for example 64K, we can use a single memory location for more than one variable this is called union.
You can use the unios in the followig locations.

  1. You can share a single memory location for a variable myVar1 and use the same location for myVar2 of different data type when myVar1 is not required any more.
  2. You can use it if you want to user, for example, a long variable as two short type variables.
  3. When you dont know what type of data is to be passed to a function, and you pass union which contains all the possible data types.

Defining a Union

Union can be defined by the keyword union.

union myUnion{
	int var1;
	long var2;
};

Here we have defined a union with the name myUnion and it has two members i.e. var1 of type int and var2 of type long

Declaring the Union

We can declare the union in various ways. By taking the above example we can declare the above defined union as.

union myUnion{
	int var1;
	long var2;
}newUnion;

So newUnion will be the variable of type myUnion. We can also declare the union as

myUnion newUnion;

Initializing the Union

We can initialize the union in various ways. For example

union myUnion{
	int var1;
	long var2;
}newUnion={10.5};

or we can initialize it as

newUnion.var1= 10;

In later stages we can also initialize the var2 as well but this will over write the var1 value. Normally when we declare the union it is allocated the memory that its biggest member can occupy. So here in our example newUnion will occupy the memory which a long type variable can occupy.

What is a Structure

A structure is a convenient tool for handling a group of logically related data items. Structure help to organize complex data is a more meaningful way. It is powerful concept that we may after need to use in our program Design.

A structure is combination of different data types. Lets take the exmple of a book, if we cant to declare a book we will be thinking about the name, title, authors and publisher of the book and publishing year. So to declare a book we need to have some complex data type which can deal with more than one data types. Lets declare a Book.

Defining a Structure

We can declare a structure to accommodate the book.

struct Book
{
	char Name[100];
	char Author[100];
	char Publisher[80];
	int Year;
};

The keyword struct defines a book, and each line with in the braces defines the elements of the Book. Now when ever we create an instance of Book it will have all the elements of the structure i.e. Name, Author, Publisher and Year.

Declaring a Structure

We can declare the structure just like union.

Book CProgrammingBook;

Initializing a Structure

Lets initialize the structure variable CProgrammingBook.

Book CProgrammingBook =
{
	"Beginning VC++ 6",
	"Ivor Horton",
	"Wrox",
	2001
};

Accessing the members of a Structure

We can access all the members of a structure by adding a period after the name of the structure variable name and then the name of the field we want to access. For example we want to change the Publish year from 2001 to 2002, we will do
it as

CProgrammingBook.Year = 2002;

Pages: [Page - 1] [Page - 2] [Page - 3] [Page - 4] [Page - 5]

Make a Comment

Make A Comment: ( 28 so far )

You must be logged in to post a comment.

28 Responses to “ Unions and Structures ”

RSS Feed for C and C++ Programming Resources Comments RSS Feed

  1. avatar azil Says:

    Please send me a code about “passing structure to a function”.

  2. avatar jagdeep Says:

    Draw a daigram internal storage of structure in c language

    my id :- ranjeet_singh_20012001 at yahoo dot com
    plz … send reply

  3. avatar shaky Says:

    Please send more information about structures and unions

  4. avatar preeti Says:

    Can somebody help me by telling the difference between structures and unions with complete programming examples for this u can mail me at http://www.aman_arora89 at rediffmail dot com

  5. avatar sachin,id:goforsachin@gmail.com Says:

    Will some one plz tell me how to handle the multiple stuctures in c

  6. avatar Raj Says:

    Is there any way to compare 2 structure, i used memcmp but it doesn’t compare the structure.

    following is the code

    struct temp
    {
      int x;
      int y;
      int z;
    };
    temp x1.x2;
    x1.x = 465, x1.y = 0, x1.z = 0;
    x2.x = 222, x2.y = 0, x2.z = 0;
    
    int Ret = memcmp(&x1,&x2,sizeof(temp));
    
    value of Ret is -1 i.e x1 is less than x2.
  7. avatar Ashwati Says:

    Hi, can anyone help me to develop,in c language for Hotel Managment system.Plz reply as soon as possible.Thank You.

  8. avatar Speedy Says:

    Tell me, what has to be done. let me try.

  9. avatar JaDe FeLiCe NaHiD Says:

    Can i ask how to insert in front and rear
    Can u give me some code??

  10. avatar RaghavaReddy Says:

    Hello can any explain structure and union, I dont know any thing

  11. avatar shaky Says:

    Its excellent, but i want some more information on it. please give me sir.

  12. avatar asli melt Says:

    Thanks that is excellent. it couldn’t be more clear

  13. avatar praveen Says:

    sir I have a prob in UNION REGS func in LINUX
    can you please give me sol

  14. avatar Samira Says:

    I m facing a problem in union swap.please can anyone help me.the problem is to swap inside an union using bitwise operator!
    please help me.It is very urgent.

  15. avatar anuradha Says:

    kindly explain array of structures and structures and functions

  16. avatar anant Says:

    I didn’t get only wat it means…
    Can u tel it in brief?
    ——– Original Message ——–
    I m facing a problem in union swap.please can anyone help me.the problem is to swap inside an union using bitwise operator!
    please help me.It is very urgent.

  17. avatar ahsun Says:

    What type of problem are you facing? Can you please tell us briefly about your problem?

    ——– Original Message ——–
    sir I have a prob in UNION REGS func in LINUX
    can you please give me sol

  18. avatar meanne Says:

    how to make an inventory using structures and files??????if you know answer kindly tell me,,thanx!

  19. avatar akhtar khan Says:

    please help me to lerning file handling to make a project in c using filehandling
    please tell me how i can learn less time

  20. avatar akhtar khan Says:

    i m studying in b.tech 2nd yr now i want to make perfect in c++ and java pls help me to do this , pls tell me how i can do

  21. avatar Speedy Says:

    Read the fundamentals of C from some xyz book. Visit these types of Sites. Think about the problems of others in these sites. WORK IN YOUR SYSTEM and come with a solution. It is the only effective way of learning as far as i am concerned.

    All the best for your b.tech studies

    ——– Original Message ——–
    i m studying in b.tech 2nd yr now i want to make perfect in c++ and java pls help me to do this , pls tell me how i can do

  22. avatar nbb18 Says:

    write a program using one dimensional array that searches a number if it is found on the list of the given 5 input numbers and locate its exact location in the list…..
    SAMPLE INPUT/OUTPUT DIALOGUE:
    enter a list of numbers:
    5 4 8 2 6

    enter a number to be searhed: 2
    2 is found in location 4

    pls….help….

  23. avatar ram manohar Says:

    Write a program to add records (EMP Name and EMP Id) and display
    it, to delete a particular record. It should be in three functions one for
    adding, two for displaying, three for deleting.

  24. avatar suma Says:

    I it possible to declare a union with in structure , if possible how

  25. avatar prasu Says:

    whether unions are defined inside structures or not?

  26. avatar Mr. Dirkdan_CpE Says:

    Im a 3rd year BS Computer Engineering student of Holy Name University here in Bohol, Philippines and I’m lucky that we have discussed that topic which is also a program that you are looking for.
    We discussed a lot about searching. No need for dimensional arrays. There is an algorithym for that, In fact there are lots of searching method that would exactly fit with your purpose of the sai program.
    If you want my friend. I can help you.. plz tell me i f you want the codes… i can send you my code! Im also in dirkdan_08@yahoo.com
    Tnx…

    ——– Original Message ——–
    write a program using one dimensional array that searches a number if it is found on the list of the given 5 input numbers and locate its exact location in the list…..
    SAMPLE INPUT/OUTPUT DIALOGUE:
    enter a list of numbers:
    5 4 8 2 6

    enter a number to be searhed: 2
    2 is found in location 4

    pls….help….

  27. avatar Mr. Dirkdan_CpE Says:

    Im just hoping that this room will actualy help me so much. Hm, Im Roldan of Philippines still studying Computer Enginnering and we’re given task to make a data structure program that has a menu an in menu.. we can have a choice to select options by which each option realy have huge size of programming codes.
    Like Array of LInked List, how should i put that program in the whole fuse program in the menu.
    Plz help me how to do this.
    For more clarrification, here is an output illustration:

    / ****MENU*****
    Press:
    1 to insert node at the beginning.
    2 to insert node at the middle.
    3 to delete the node.
    4 for array of linked list
    5 for doubly link list
    6. for circularly linked list
    7 append linked list
    8 to exit./

    so plz me here. Send me best websites that will give best and fit answers for my problem. Hope you can help me here..
    The core of my problem is the concept of fusing or inserting the huge programs like circularly in a one program…

    Thank you and Godbless
    Plz do replies coz i need it this week!

  28. avatar parsant tanwar Says:

    hi roldan dear if u have got reply or answer of this question plz provide me also those link or solution

    i m parsant tanwar an engineering student from india.

    i m not able to understand link list concept properly up to now

    so please provide me appropriate solution

    thank you

    have a good day

Where's The Comment Form?