Unions and Structures

What is a Union?

A union is a user defined variable which may hold members of different sizes and type. Union uses a single memory location to hold more than one variable. We can use the unions in the following locations.

  1. 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. Use it if you want to user, for example, a long variable as two short type variables.
  3. We don’t 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.

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.

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

Initializing the Union

We can initialize the union in various ways. For example

or we can initialize it as

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 example 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.

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.

Initializing a Structure

Lets initialize the structure variable CProgrammingBook.

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

30 Comments

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

    following is the code

    • Looks to me like memcmp() worked just fine. It returned a non-zero value because x1 and x2 are not equal. If x1 and x2 would be equal, then memcmp() should return 0.

    • 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

  2. 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.

    • 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.

    • 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

  3. 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….

    • 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….

  4. 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.

  5. 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!

    • 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

Leave a Reply