0% found this document useful (0 votes)
7 views16 pages

Unit 4

Uploaded by

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

Unit 4

Uploaded by

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

21CB101 PROBLEM

SOLVING AND C
PROGRAMMING
UNIT-4
STRUCTURES AND UNION
DEFINITION
• Used for handling a group of logically related data items
• Examples:
Student name, roll number, and marks
• Real part and complex part of a complex number
• Helps in organizing complex data in a more meaningful way
• The individual structure elements are called members
struct tag {
member 1;
member 2;
:
member m;
};
– struct is the required C keyword
– tag is the name of the structure
– member 1, member 2, … are individual member declarations
• The individual members can be ordinary variables,
pointers, arrays, or other structures (any data type)
– The member names within a particular structure must
be distinct from one another
– A member name can be the same as the name of a
variable defined outside of the structure
• Once a structure has been defined, the individual structure-
type variables can be declared as:
struct tag var_1, var_2, …, var_n;
ARRAY OF STRUCTURES
• Once a structure has been defined, we can
declare an array of structures
struct student class[50];

– The individual members can be accessed as:


class[i].name
class[5].roll_number
NESTED STRUCTURE
#include <stdio.h>
#include <string.h>
struct Employee
{
int id;
char name[20];
struct Date
{
int dd;
int mm;
int yyyy;
}doj;
}e1;
Cont…
int main( )
{
//storing employee information
e1.id=101;
strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array
e1.doj.dd=10;
e1.doj.mm=11;
e1.doj.yyyy=2014;

//printing first employee information


printf( "employee id : %d\n", e1.id);
printf( "employee name : %s\n", e1.name);
printf( "employee date of joining (dd/mm/yyyy) : %d/%d/%d\
n", e1.doj.dd,e1.doj.mm,e1.doj.yyyy);
return 0;
}
OUTPUT
• employee id : 101
• employee name : Sonoo Jaiswal
• employee date of joining (dd/mm/yyyy) :
10/11/2014
Self Referential structures
• Self Referential structures are those structures that have
one or more pointers which point to the same type of
structure, as their member.
• structures pointing to the same type of structures are self-
referential in nature.
• Self-referential structure plays a very important role in
creating other data structures like Linked list.
• In this type of structure, the object of the same structure
points to the same data structure and refers to the data
types of the same structure.
• It can have one or more pointers pointing to the same type
of structure as their member.
• The self-referential structure is widely used in dynamic
data structures such as trees, linked lists, etc.
SINGLE LINKED LIST
• A linked list is a useful data storage method, and it is very easy
to implement it in C programming Language.
• Several kinds of linked lists, including single linked lists,
double linked lists, and binary trees.
• These structures consist of a single Link, mainly the only one
pointer link of structure type pointing to the same structure. To
better understand it, we can connect this concept with the singly
linked list.
• In a singly linked list, there is only a single pointer that carries
the address of the next node, and that pointer variable is of the
structure node type itself. It is mainly used when we want to
store the different data items by fetching out them from various
addresses and connecting all of them by storing the address of
one data item into the linked part of the other node. In this way,
we can easily connect all the data items by using these
connecting links.
DYNAMIC MEMORY
ALLOCATION
• Dynamic memory allocation
– Know how much memory is needed after the program
is run
• Example: ask the user to enter from keyboard
– Dynamically allocate only the amount of memory
needed
• C provides functions to dynamically allocate
memory
– malloc, calloc, realloc
MEMORY ALLOCATION
FUNCTIONS
• malloc
– Allocates requested number of bytes and returns a
pointer to the first byte of the allocated space
• calloc
– Allocates space for an array of elements, initializes
them to zero and then returns a pointer to the memory.
• free
– Frees previously allocated space.
• realloc
– Modifies the size of previously allocated space.
UNION
• The Union is a user-defined data type in C language that can
contain elements of the different data types just like structure.
• But unlike structures, all the members in the C union are stored in
the same memory location.
• Storage Classes are used to describe the features of a
variable/function. These features basically include the scope,
visibility, and lifetime which help us to trace the existence of a
particular variable during the runtime of a program.
STORAGE CLASSES
Cont…
• Auto variables can be only accessed within the
block/function they have been declared and not outside
them (which defines their scope).
• Extern storage class simply tells us that the variable is
defined elsewhere and not within the same block where it
is used.
 It can be accessed within any function/block.
• Static variables have the property of preserving their value
even after they are out of their scope!
• Register storage class declares register variables that have
the same functionality as that of the auto variables.
 The only difference is that the compiler tries to store
these variables in the register of the microprocessor if a
free register is available.

You might also like