structure-c-notes
structure-c-notes
Structure
A structure is a user defined data type. We know that arrays can be used to
represent a group of data items that belong to the same type, such as int or float. However
we cannot use an array if we want to represent a collection of data items of different types
using a single name. A structure is a convenient tool for handling a group of logically related
data items.
Structure is a user defined data type used to represent a group of data items of
different types using a single name.
struct student
{
int rollno;
char name[25];
float totalmark;
};
struct structure_name
{
type element 1;
type element 2;
……………..
type element n;
} var1,var2,…,varn;
The following single declaration is equivalent to the two declaration presented in the
previous example.
struct student
{
int rollno;
char name[25];
float totalmark;
} stud1, stud2;
Example:
In the above example stud1 is a structure variable of type student. To access the
member name, we would write stud1.name. Similarly, stud1’s rollno and stud1’s totalmark
can be accessed by writing stud1.rollno and stud1.totalmark respectively.
Output
STUDENTS DETAILS:
Roll number: 1
Name: Venkat
Total Marks:98.000000
Roll number: 2
Name: Arpita
Total Marks:99.000000
Roll number: 2
Name:Shweta
Total Marks:99.000000
Array of structures:
It is possible to store a structure has an array element. i.e., an array in which each
element is a structure. Just as arrays of any basic type of variable are allowed, so are arrays
of a given type of structure. Although a structure contains many different types, the
compiler never gets to know this information because it is hidden away inside a sealed
structure capsule, so it can believe that all the elements in the array have the same type,
even though that type is itself made up of lots of different types.
Union
Union is a user created data type similar to structure but in this case all the members
share a common memory location. The size of the union corresponds to the length of the
largest member. Since the member share a common location they have the same starting
address.
For example, the following code declares a union data type called Student and a
Vidya Pawar, Vedanta Degree College, Raichur
Page 4
Structures and Unions
union variable called stud:
union student
{
int rollno;
float totalmark;
};
union student stud;
It is possible to combine the declaration of union combination with that of the union
variables, as shown below.
union union_name
{
type element 1;
type element 2;
……………..
type element n;
}var1,var2,…,varn;
The following single declaration is equivalent to the two declaration presented in the
previous example.
union student
{
int rollno;
float totalmark;
}x,y,z;
Structure Union
The amount of memory required to store a The amount of memory required is always equal
structure variable is the sum of the size of all the to that required by its largest member.
members.
Each member have their own memory space. One block is used by all the member of the
union.
Within a structure all members gets While retrieving data from a union the
memory allocated; therefore any type that is being retrieved must be
member can be retrieved at any time. the type most recently stored. It is the
programmer's responsibility to keep
track of which type is currently stored
in a union; the results are
implementation-dependent if
something is stored as one type and
extracted as another.