1. Structures
• A structure is a collection of one or more variables types
grouped under a single name for easy manipulation.
• The variables in a structure, unlike those in an array, can
be of different variable types.
• A structure can contain any of C's data types, including
arrays and other structures.
• Each variable within a structure is called a member of
the structure.
2.
3.
4. Using a struct
• By defining a structure you create a new data
type.
• Once a struct is defined, you can create
variables of the new type.
Student stu_variable;
5. Accessing Structure Members
• Structure members are accessed using the
structure member operator (.), also called the dot
operator, between the structure name and the
member name.
• Thus variable s1 can be access in following way
S1.no=10;
S1.name=”AMAR”;
S1.per=80;
• printf("%d,%s,%f", s1.no, s1.name,s1.per);
7. Advantage Of Using structure
• One major advantage is that you can copy
information between structures of the same type
with a simple equation statement.
• Continuing with the preceding example, the
statement
S2 = S1;
is equivalent to this statement:
S2.no=S1.no;
S2.name=S1.name;
S2.per=S1.per;
11. UNIONS
• A union is a variable which may hold members of
different sizes and types.
• The syntax for declaring a union is similar to that of
a structure:
union number
{
int number;
double floatnumber;
} anumber;
12. • This defines a union called "number" and a
variable of it called "anumber".
• Members of the union can be accessed in the
following way:
• printf("%f",anumber.floatnumber);
• When the C compiler is allocating memory for
unions it will always reserve enough room for
the largest member.