0% found this document useful (0 votes)
2 views

structure-c-notes

The document explains structures and unions in programming, highlighting that structures are user-defined data types that can hold a collection of different data types, while unions allow multiple members to share the same memory location. It details the syntax for declaring structures and unions, accessing their members, and initializing them. Additionally, it compares the memory usage and initialization rules between structures and unions.

Uploaded by

Rehaan Zaina
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

structure-c-notes

The document explains structures and unions in programming, highlighting that structures are user-defined data types that can hold a collection of different data types, while unions allow multiple members to share the same memory location. It details the syntax for declaring structures and unions, accessing their members, and initializing them. Additionally, it compares the memory usage and initialization rules between structures and unions.

Uploaded by

Rehaan Zaina
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Structures and Unions

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.

The syntax of structure declaration is


struct structure_name
{
type element 1;
type element 2;
……………..
type element n;
};
In structure declaration the keyword struct appears first, this followed by structure
name. The member of structure should be enclosed between a pair of braces and it defines
one by one each ending with a semicolon. It can also be array of structure. There is an
enclosing brace at the end of declaration and it end with a semicolon.

We can declare structure variables as follows


struct structure_name var1,var2,…..,var n;
Example:
To store the names, roll number and total mark of a student you can declare 3
variables. To store this data for more than one student 3 separate arrays may be declared.
Another choice is to make a structure. No memory is allocated when a structure is declared.
It just defines the “form” of the structure. When a variable is made then memory is
allocated. This is equivalent to saying that there's no memory for “int”, but when we declare
an integer that is int var; only then memory is allocated. The structure for the above-
mentioned case will look like

struct student
{
int rollno;
char name[25];
float totalmark;
};

We can now declare structure variables stud1, stud2 as follows


struct student stud1,stud2;

Vidya Pawar, Vedanta Degree College, Raichur


Page 1
Structures and Unions
Thus, the stud1 and stud2 are structure variables of type student. The above
structure can hold information of 2 students.

It is possible to combine the declaration of structure combination with that of the


structure variables, as shown below.

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;

Accessing structure Variable


The different variable types stored in a structure are called its members. The
structure member can be accessed by using a dot (.) operator, so the dot operator is known
as structure member operator.

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.

Initializing Structure Members


Structure members can be initialized at declaration. This much the same manner as
the element of an array; the initial value must appear in the order in which they will be
assigned to their corresponding structure members, enclosed in braces and separated by
commas.
The general form is
struct structure_name var={val1,val2,val3…..};
Example:
#include <stdio.h>

Vidya Pawar, Vedanta Degree College, Raichur


Page 2
Structures and Unions
#include<conio.h>
void main()
{
struct student
{
char *name;
int rollno;
float totalmark;
};
struct student stud1={"Venkat",1,98};
struct student stud3= {"Shweta",3,97};
struct student stud2={"Arpita",2,99};
clrscr();
printf(“STUDENTS DETAILS:\n”);
printf(“\n\n Roll number:%d\n Name:%s\n Total Marks:%f”, stud1.rollno, stud1.name,
stud1.totalmark);
printf(“\n\n Roll number:%d\n Name:%s\n Total Marks:%f”, stud2.rollno, stud2.name,
stud2.totalmark);
printf(“\n\n Roll number:%d\n Name:%s\n Total Marks:%f”, stud3.rollno, stud3.name,
stud3.totalmark);
getch();
}

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.

The declaration statement is given below.


Vidya Pawar, Vedanta Degree College, Raichur
Page 3
Structures and Unions
struct struct_name
{
type element 1;
type element 2;
……………..
type element n;
}array name[size];

Example: struct student


{
int rollno;
char name[25]; float totalmark;
)} stud[100];

In this declaration stud is a 100-element array of structures. Hence, each element of


stud is a separate structure of type student. An array of structure can be assigned initial
values just as any other array. So the above structure can hold information of 100 students.

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.

The real purpose of unions is to prevent memory fragmentation by arranging for a


standard size for data in the memory. By having a standard data size we can guarantee that
any hole left when dynamically allocated memory is freed will always be reusable by
another instance of the same type of union. This is a natural strategy in system
programming where many instances of different kinds of variables with a related purpose
and stored dynamically.
A union is declared in the same way as a structure. The syntax of union declaration is
union union_name
{
type element 1;
type element 2;
……………..
type element n;
};

This declares a type template. Variables are then declared as:


union union_name x,y,z;

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;

Vidya Pawar, Vedanta Degree College, Raichur


Page 5
Structures and Unions

Exercise: Compare structure and Union

The difference between structure and union is,

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.

Keyword struct defines a structure Keyword union defines a union.


struct s_tag union u_tag
{ {
int ival; int ival;
float fval; float fval;
char *cptr; char *cptr;
}s; }u;

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.

One or more members of a structure A union may only be initialized with a


can be initialized at once. value of the type of its first member;
thus union u described above (during
example declaration) can only be
initialized with an integer value.

Vidya Pawar, Vedanta Degree College, Raichur


Page 6

You might also like