Unions and Structures
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.
- 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.
- You can use it if you want to user, for example, a long variable as two short type variables.
- 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]
Tags: C Programming, Structures, Union
There are 28 Comments to this post. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response or TrackBack from your own site.
Please send me a code about “passing structure to a function”.
Draw a daigram internal storage of structure in c language
my id :- ranjeet_singh_20012001 at yahoo dot com
plz … send reply
Please send more information about structures and unions
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
Will some one plz tell me how to handle the multiple stuctures in c
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.Hi, can anyone help me to develop,in c language for Hotel Managment system.Plz reply as soon as possible.Thank You.
Tell me, what has to be done. let me try.
Can i ask how to insert in front and rear
Can u give me some code??
Hello can any explain structure and union, I dont know any thing
kindly explain array of structures and structures and functions
Its excellent, but i want some more information on it. please give me sir.
Thanks that is excellent. it couldn’t be more clear
sir I have a prob in UNION REGS func in LINUX
can you please give me sol
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
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.
how to make an inventory using structures and files??????if you know answer kindly tell me,,thanx!
please help me to lerning file handling to make a project in c using filehandling
please tell me how i can learn less time
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
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
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….
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.
I it possible to declare a union with in structure , if possible how
whether unions are defined inside structures or not?
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