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

Structure and Union

This document discusses structures in C programming. It defines a structure as a user-defined data type that allows storing related data of different types together under a single name. The key points are: 1. A structure declaration uses the struct keyword followed by the structure name and variables of different data types. 2. Structure variables can be initialized by assigning values to members in curly braces. 3. Individual members of a structure can be accessed using the dot operator. 4. The size of a structure is the total size of all its members. The sizeof operator can be used to determine a structure's size. 5. Unions are similar to structures but can only store data in one member

Uploaded by

Sahana M.k
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Structure and Union

This document discusses structures in C programming. It defines a structure as a user-defined data type that allows storing related data of different types together under a single name. The key points are: 1. A structure declaration uses the struct keyword followed by the structure name and variables of different data types. 2. Structure variables can be initialized by assigning values to members in curly braces. 3. Individual members of a structure can be accessed using the dot operator. 4. The size of a structure is the total size of all its members. The sizeof operator can be used to determine a structure's size. 5. Unions are similar to structures but can only store data in one member

Uploaded by

Sahana M.k
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

5.

1 Introduction
Structure is basically a user-defined data type that can store related information (even of different
data types) together.
V The major difference between a structure and an array is that an array can store only information of
same data type.
A structure is a collection of variables under a single name. The variables within a structure are
of different data types and each has a name that is used to select it from the structure.
*A Structure is a user defined data type, which is used to store the values of different data types
together under the same name".

5.1.1Structure Declaration
A structure is declared using the keyword struct followed by the structure name.
Allthe variables of the structure are declared within the structure.
A structure type is generally declared by using the following syntax:

struct struct-name
struct student studi;

data type var-name;


Jrno name

data type var name;


cOurse

fees

struct student stud2;


For example: r_no
name
struct student
COurse

int r_no;
fees
char name[20):
char course[20]:; Figure 5.1 Memory allocation for a structure
float fees; variable

VA variable of structure student can be defined by writing:


struct student studl;
struct student is a data type and studl is a variable.
In the following syntax, the variables are declared at the time of structure declaration.
struct student

int r no;
char name[20];
char course[20]:
float fees;
}studl, stud2;
studl and stud2 of the structure student.
5.1.3 Initialization of Structures
Initializing a structure means assigning some constants to the members of the structure.
When the user does not explicitly initialize the structure, then C automatically does it.
For int and float members, the values are initialized to zero, and char and string members are
initialized to "\0' by default.
V The initializers are enclosed in braces and are separated by commas.
V The general syntax to initialize a structure variable is as follows:
struct struct name

data _type member namel;


data_type member name2;
data type member_name3;
}struct_var = {constant1, constant2, constant3,...};
Or
struct struct name

data_type member namel;


data_type member_name2;
data _type member name3;

struct struct_name struct_var = (constant1, constant2, constant 3,...;


For example, we can initialize a student structure by writing,
struct student

int r_no;
char name(20];
char course|20|:
float fees:
}studl = {01, "Rahul'", "BCA", 45000;;
Or.
by writing,
struct student studl = {01, "Rahul", "BCA", 45000};
struct student stud1 struct student stud2 {07, "Rajiv"};
- (01, "Rahul", "BCA", 45000):
0 Rahul BCA 45000 07 Rajiv 10 0.0

r no name course fees r_no name course fees

5.1.4 Accessing the Members of a Structure


V Astructure member variable is generally accessed using a '." (dot) operator. The syntax of accessing
a structure or a member of a structure can be given as:
struct_var. member_name
For example, to assign values to the individual data members of the structure variable studl, we
may write
studl.r _no =01;
studl.name = "Rahul":
studl.course = "BCA";
stud1.fees = 45000;
To input values for data members of the structure variable studl, we may write
scanf("%od", &studl.r_no);
scanf("os", stud1.name);
V Similarly, to print the values of structure variable stud1, we may write
printf("l%s", stud1.course);
printf("%", studl.fees):

5.1.5 Copying and Comparing Structures struct student studi


- (01, "Rahul", "BCA", 45000);
We can assign a structure to another structure of the same type:
Rahul BCA |45000
Then to assign one structure variable to another, we will write
01
r_no name cOurse fees
stud2 = studl;
struct student stud2 = stud1:
VC does not permit comparison of one structure variable with
another. However, individual members of one structure can be 01 Rahul BCA 45000
compared with individual members of another structure. r_no name course fees

For example, to compare the fees of two students, we will write


if(stud1.fees > stud2. fees) //to check if fees of studl is greater than stud2

5.1.6 Finding the Size of the structures


There are two different ways through which we can find the number of bytes a structure will occupy
in the memory.

1. Simple Addition
In this techniques, make a list of all data types and add the memory required by each.
Consider a simple structure of an employee
struct Employee

int emp id;


char name(20];:
double salary;
char designation[20];
float experience,
Size =size of emp_ id+ size of name +size of salary +size of designation +size of experience
Size of emp id=2
Size of name =20* size of character
Size of salary =8
Size of designation = 20 * Size of character
Size of experience = 4
Therefore, Size =2 + 20*1+8 +20 *1+4
=2+20+8+20+4
= 54bytes
2. Using sizeof operator
V The sizeof operator is used to calculate the size of a data type, variable, or an expression.
This operator can be used as follows:
sizeof(struct_name);
Ex:
#include<stdio.h>
struct employee
int emp_id;
char name[10];
double salary;
char designation [20]:
float experience;

void main()
struct employee e;
printf("%d", sizeof(e);
}
Example: C program to read and display the student details using structures.
#include<stdio.h>
struct student

int rnum;
char name[20]:
int marks;
}s[60):
void main()

int in;
printf("Enter the number of students"):
5.3 Union
Like structure, a union is a collection of variables of diferent data types. The only difference
between a structure and a union is that in case of unions, you can only store information in one field
at any one time.
To better understand union, think of it as a chunk of memory that is used to store variables of different
types. When a new value is assigned to a field, the existing data is replaced with the new data.

5.3.1 Declaring a Union


V The syntax for declaringa union is same as that of declaring a structure.
union union-name

data tvpe var-name;


data _type var-name;

/ Again, the typedef keyword can be used to simplify the declaration of union variables.
The most important thing to remember about a union is that the size of an union is the size of its
largest field. This is because a sufficient number of bytes must be reserved to store the largest sized
field.

5.3.2 Accessing a Member of a Union


V A member of aunion can be accessed using the same syntax as that ofa structure.
V To access the fields of a union, use the dot operator(.).
V That is the union variable name followed by the dot operator followed by the member name.
5.3.3 Initializing Unions
Itis an error to initialize any other union member except the first member.
VA striking difference between a structure and a union is that in case of a union, the fields share the
same memory space, so ffresh data replaces any existing data. Look at the code given below and
observe the difference between a structure and union when their fields are to be initialized.
#include<stdio.h>
typedef struct POINTI

Int x, y,

typedef union POINT2

int x;
int y;

You might also like