0% found this document useful (0 votes)
14 views35 pages

Structure and Union

Uploaded by

Saky Hasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views35 pages

Structure and Union

Uploaded by

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

CSE 1102

Structure and
Union
What is Structure
a ‘book’ is a collection of things such as title, author, call number,
publisher, number of pages, date of publication, etc
all this data is dissimilar
author is a string, whereas number of pages is an integer

11/07/2024 2
What is Structure
For dealing with such collections, C provides a data type called
‘structure’
A structure gathers together, different atoms of information that
comprise a given entity
We have seen earlier how ordinary variables can hold one piece of
information
how arrays can hold a number of pieces of information of the same
data type

11/07/2024 3
What is Structure
These two data types can handle a great variety of situations
But quite often we deal with entities that are collection of dissimilar
data types

11/07/2024 4
What is structure
to store data about a book. You might want to store
1. its name (a string)
2. its price (a float)
3. number of pages in it (an int)

11/07/2024 5
What is Structure
we can follow two approaches
1. Construct individual arrays, one for storing names, another for
storing prices and still another for storing number of pages.
2. Use a structure variable.

11/07/2024 6
What is structure
Let us examine these two approaches one by one

11/07/2024 7
Making things with Arrays

11/07/2024 8
Making things with Structures

11/07/2024 9
Declaration of Structure and
Structure Variables

11/07/2024 10
Declaration of Structure and
Structure Variables
The general format of structure definition is:

11/07/2024 11
Declaration of Structure and
Structure Variables
In can be also written as--

11/07/2024 12
Initializing Structure Variables

11/07/2024 13
Accessing Structure Elements

11/07/2024 14
Accessing Structure Elements

11/07/2024 17
Array of Structures

11/07/2024 18
11/07/2024 19
11/07/2024 20
11/07/2024 21
Copying structure elements (Piece-
meal copying vs Copying as a whole)

11/07/2024 22
Nested Structures

11/07/2024 23
Passing structure variables

11/07/2024 24
Passing structure variables

11/07/2024 25
Structure Pointers

11/07/2024 26
Call by reference with structures

11/07/2024 27
User-Defined Data Types
(typedef)

where type refers to an existing data type (either a standard


data type, or previous user-defined data type), and new- type
refers to the new user-defined data type.
Example:
typedef int age;
age male, female;

11/07/2024 28
User-Defined Data Types
(typedef)

where new- type is the user-defined


structure type. typedef struct students
{
char name[50];
char branch[50];
int ID_no;
} students;
students st;

11/07/2024 29
SELF-REFERENTIAL
STRUCTURES

11/07/2024 30
#include <stdio.h>

struct node {
int data1;
int data2;
struct node* link;
};

int main()
{
struct node ob1; // Node1

// Initialization
ob1.link = NULL;
ob1.data1 = 10;
ob1.data2 = 20;

struct node ob2; // Node2

// Initialization
ob2.link = NULL;
ob2.data1 = 30;
ob2.data2 = 40;

// Linking ob1 and ob2


ob1.link = &ob2;

// Accessing data members of ob2 using


ob1
printf("%d", ob1.link->data1);
printf("\n%d", ob1.link->data2);
return 0;
}

11/07/2024 31
Unions
Unions, like structures, contain members whose individual data types may
differ from one another
The members within a union all share the same storage area within the
computer’s memory, whereas each member within a structure is assigned its
own unique storage area.
They are useful for applications involving multiple members, where values
need not be assigned to all of the members at any one time.
The user must keep track of what type of information is stored at any given
time.
An attempt to access the wrong type of information will produce meaningless
results.
11/07/2024 33
Unions

11/07/2024 34
Unions

11/07/2024 35
11/07/2024 36
11/07/2024 37
References
•https://www.geeksforgeeks.org/structures-c/
•https://www.w3schools.com/c/c_structs.php
•https://www.programiz.com/c-programming/c-structures
•https://www.tutorialspoint.com/cprogramming/c_structures.htm
•https://www.javatpoint.com/structure-in-c

11/07/2024 38

You might also like