Module 4 - Structure & Union
Module 4 - Structure & Union
Dr. Suguna M
Assistant Professor Senior Grade ,
School of Computer Science and Engineering,
Vellore Institute of Technology,
Data Types
Data types are defined as the data storage format that a variable can store a data.
Data types in C
C language has built-in datatypes like primary and derived data types.
But, still not all real world problems can be solved using those data types.
We need custom datatype for different situation.
User Defined Datatype
We need combination of various datatypes to understand different entity/object.
Example-1:
Book Title: Let Us C Datatype: char / string
Author: Yashavant Kanetkar Datatype: char / string
Page: 320 Datatype: int
Price: 255.00 Datatype: float
Example-2:
Student Name: ABC Datatype: char / string
Roll_No: 180540107001 Datatype: int
CPI: 7.46 Datatype: float
Backlog: 01 Datatype: int
What is Structure?
Structure is a collection of logically related data items of different datatypes
grouped together under single name.
Structure is a user defined datatype.
Structure helps to build a complex datatype which is more meaningful than an
array.
But, an array holds similar datatype record, when structure holds different
datatypes records.
Two fundamental aspects of Structure:
Declaration of Structure Variable
Accessing of Structure Member
Syntax to Define Structure
To define a structure, we need to use struct keyword.
This keyword is reserved word in C language. We can only use it for structure and
its object declaration.
Syntax
1 struct structure_name structure_name is name of custom type
2 {
3 member1_declaration;
4 member2_declaration;
5 . . .
memberN_declaration is individual
member
6 memberN_declaration;
declaration
7 };
Example
1 struct student
2 {
3 char name[30]; // Student Name
4 int roll_no; // Student Roll No
5 float CPI; // Student CPI
6 int backlog; // Student Backlog
7 };
8 struct student student1; // Declare structure variable
Access Structure member (data)
Structure is a complex data type, we cannot assign any value directly to it
using assignment operator.
We must assign data to individual structure members separately.
C supports two operators to access structure members, using a structure variable.
1. Dot/period operator (.)
2. Arrow operator (->)
Access Structure member (data) – Cont.
1. Dot/period operator (.)
It is known as member access operator. We use dot operator to access members of simple
structure variable.
Syntax Example
1 structure_variable.member_name; 1 // Assign CPI of student1
2 student1.CPI = 7.46;
2. Arrow operator (->)
In C language it is illegal to access a structure member from a pointer to structure variable
using dot operator.
We use arrow operator to access structure member from pointer to structure.
Syntax Example
1 pointer_to_structure->member_name; 1 // Student1 is a pointer to student type
2 student1 -> CPI = 7.46;
Write a program to read and display student information using structure.
• Example:
• emp1.emp_num, emp2.name
Output:
Displaying data
emp number: 123
emp name: Ganga
emp salary: 13200.00
Nested structure
• A structure can have as its member a structure variable.
• Example:
struct name{
char fname[10];
char lname[10];
};
struct student{
struct name nm;
int rollno;
} stud; Note: another way of
declaring structure variable
main(){
printf("enter first and last name: ");
scanf("%s%s", stud.nm.fname,stud.nm.lname);
printf("\nenter roll number:");
scanf("%d", &stud.rollno);
printf("displaying data\n");
printf("Name:%s %s\n", stud.nm.fname,stud.nm.lname);
printf("roll no %d\n", stud.rollno);}
Output:
enter first and last name: John Ray
enter roll number:12345
displaying data
Name:John Ray
roll no 12345
Unions
• Union allows variables of different data types to share the same
memory space.
• For example, a variable of int type and a variable of type char share
same memory space. That means this memory space can be treated
either as int or as char.
union X{
a
short a;
char c[2];
}; 1000 c[0] 1001 c[1] 1002
main(){ un.c
union X{
Output:
short int a;
16449
char c[2];} x1;
A
x1.a=16449; @
printf("%d\n",x1.a);
Output depends on
printf("%c\n",x1.c[0]); the endianess of the
printf("%c\n",x1.c[1]); } system you are using
0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 0
Big endian
Difference between struct and union
// C program to illustrate differences between structure and Union
#include <stdio.h>
#include <string.h>
// declaring structure
struct struct_example
{
int integer;
float decimal;
char name[20];
};
// declaraing union
union union_example
{
int integer;
float decimal;
char name[20];
};
void main()
{
// creating variable for structure
// and initializing values difference
// six
struct struct_example s={18,38,"geeksforgeeks"};
// difference five
printf("\n Accessing all members at a time:");
s.integer = 183;
s.decimal = 90;
strcpy(s.name, "geeksforgeeks");
u.integer = 183;
u.decimal = 90;
strcpy(u.name, "geeksforgeeks");
printf("\nstructure data:");
s.integer = 240;
printf("\ninteger: %d", s.integer);
s.decimal = 120;
printf("\ndecimal: %f", s.decimal);
u.decimal = 120;
printf("\ndecimal: %f", u.decimal);
u.integer = 1218;
printf("union data:\n integer: %d\n"
" decimal: %.2f\n name: %s\n",
u.integer, u.decimal, u.name);
}
OUTPUT:
Structure Data:
Integer :18
Decimal: 38
name: geeksforgeeks
union data:
integeer: 18
decimal: 0.00
name: ?
sizeof structure: 28
sizeof union: 20
Accessing all members at a time:
structure data:
integer: 183
decimal: 90.00
name: geeksforgeeks
union data:
integeer: 1801807207
decimal: 277322871721159510000000000.00
name: geeksforgeeks
Accessing one member at a time:
structure data:
integer: 240
decimal: 120.000000
name: C programming
union data:
integer: 240
decimal: 120.000000
name: C programming
Altering a member value:
structure data:
integer: 1218
decimal: 120.00
name: C programming
union data:
integer: 1218
decimal: 0.00
name: ?