Nothing Special   »   [go: up one dir, main page]

12-Structures and Unions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Structures and Unions

Dr. Mayank Swarnkar

Department of Computer Science and Engineering


Indian Institute of Technology (BHU) Varanasi
mayank.cse@iitbhu.ac.in

Week-12

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-12 1 / 17


Introduction

Different data items of same data types:


We studied arrays
Different data items of different data types, we will study:
Structures
Unions
Allows complex data organization in a meaningful way

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-12 2 / 17


Structures

Keyword: struct
Structure also has:
Declaration (template)
Definition
Declaration of example structure named as library:

struct library
{
char title[20];
char author[15];
int pages;
float price;
};

Figure: Example Structure

Exercise: Distinguish between Array and Structure

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-12 3 / 17


Declaration of Structure Variables

struct library struct library


{ {
char title[20]; char title[20];
char author[15]; char author[15];
int pages; int pages;
float price; float price;
}; }book1, book2, book3;

struct library book1, book2, book3;

Figure: Declaration of Structure Variables

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-12 4 / 17


Accessing Structure Members

Using dot operator (.) or arrow operator (→)


Normal member then use dot operator
Pointer member then use arrow operator
Examples:
book1.price
if title is pointer type then book1→title
struct library book1 = {“My Book”, “Hi”, 100, 52.7};
Comparison in structures:
Two strcut cannot be compared directly
Comparison need to be made by comparing members using (.,→)

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-12 5 / 17


Exercise

Create an inventory of 5 students with following parameters:


Student Name (take pointer char array)
Student ID (take integer of exact 5 digits)
Student JEE Score (take integer but less then or equal to 300)
Class XII percentage (float but less than 100)
Merit list will be printed with following rules:
If more than 5 student data is entered
If JEE Score > 150 and Class XII percentage > 85 then merit
Print in the following format:
Student Name < tab > Student ID < tab > merit/non merit

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-12 6 / 17


Arrays of Structures

Example:
struct library book[2] = {{“B1”,“T1”,10,5.0}, {“B2”,“T2”,20,12.5}}
book[0].price = 25.0
book[1].pages = 70

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-12 7 / 17


Arrays within Structures

Example:
book[0].title[0]
This will return first character of title of first book

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-12 8 / 17


Structures within Structures

struct salary
{
char name[10];
char department[10];
struct
{
int da;
int hra;
}
allowance;
}
employee;

Figure: Structures within Structures

Accessing inner structure:


employee.allowance.da = 5000;
C permits nesting upto 63 levels

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-12 9 / 17


Structures and Functions

Structure values are passed as arguments in functions


Three methods:
Pass each member separately
Becomes inefficient for large structures
Pass copy of entire structure
return structure only
All compilers do not have this support
Pass base address of structure using pointers
Function can indirectly access whole structure
Considered most efficient

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-12 10 / 17


Exercise
Create an inventory of 50 students (use array of structures) with
following parameters:
Student Name (Using random string generation)
Student ID (Using random number generation of exact 5 digits)
Student JEE Score (Using random number generation ≤ 300)
Class XII percentage (Using random number generation ≤ 100)
Merit list will be printed with following rules:
If JEE Score ∈ [200,300] and Class XII % ∈ [90,100] then 100%
scholarship
If JEE Score ∈ [100,199] and Class XII % ∈ [80,89.9] then 50%
scholarship
else no scholarship
Print in the following format:
Student Name < tab > Student ID < tab > 100% scholarship/50%
scholarship/no scholarship
Also print after that:
Average JEE Score of all students (use function to calculate average)
Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-12 11 / 17
Union
Concept borrowed from structures
Follows same syntax of structures
Major difference is storage:
Structure: Each member has its own storage location
Union: Each member shares common storage area
Storage location of largest size member
Union can handle only one member at a time

union item
{
int m; m
float x;
char c; x
} code;
c

Figure: Union Memory Allocation

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-12 12 / 17


Accessing Members in Union

Same as structures
Only advantage over structure is memory efficiency
Follow this rule:
Access the member whose value is currently stored
Try following example:

code.m = 500;
code.x = 3.14;
printf("%d\n", code.m);

Figure: Example

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-12 13 / 17


sizeof Keyword

Finds the size of any data type


format: sizeof(variable-name);
Exercise: Find the size of a:
Structure
Union

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-12 14 / 17


Bit Fields
Integer: 16 bits, Character: 8 bits, Float: 32 bits
Bit Field Bit Length Range
Married 1 0-1
Age 7 0-127
Children 4 0-15
Table: Bit Fields

Bit Field:
Structure of unsigned values
format: unsigned variable-name : bit-length
struct person
{
unsigned married : 1
unsigned age : 7
unsigned children : 4
} emp;

Figure: Example Bit Field

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-12 15 / 17


Accessing Members in Bit Fields

Just like structures direct assignment is valid:


emp.married = 1;
emp.age = 50;
Directly scanf does not work
scanf(“%d”, &AGE);
emp.age = AGE;
Bit fields cannot be accessed by pointers
Give the reason ?

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-12 16 / 17


End of Lecture

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-12 17 / 17

You might also like