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

C_Day 8

The document provides an overview of structures, unions, and dynamic memory allocation in C programming. It includes definitions, syntax, examples, and differences between structures and unions, as well as memory allocation techniques like malloc, calloc, and realloc. Additionally, it features quizzes and FAQs related to the topics covered.

Uploaded by

amanpathak160605
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)
5 views

C_Day 8

The document provides an overview of structures, unions, and dynamic memory allocation in C programming. It includes definitions, syntax, examples, and differences between structures and unions, as well as memory allocation techniques like malloc, calloc, and realloc. Additionally, it features quizzes and FAQs related to the topics covered.

Uploaded by

amanpathak160605
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/ 41

DAY 8

6/30/2024
RECAP

6/30/2024
Topics
• Structure
• Structure within structure
• Array of structure
• Union
• Difference between Structure and Union
• Dynamic memory Allocation

6/30/2024
Structure

6/30/2024
Structure
• Collection of different types of data
• struct keyword is used

6/30/2024
Defining Structure
Syntax: Example:
struct structure-name struct student
{ {
data-type member-1; int rollno;
data-type member-2; char name[20];
data-type member-3; float marks;
}; };
Declaring Structure Variables
Syntax:
struct structure-name variable-name;
Example:
struct student s;

6/30/2024
Program to read the information of a student from the keyboard
and print the same on the screen using structure.
#include<stdio.h>
#include<conio.h>
struct student
{
int roll;
int age;
char name[20];
};
void main()
{
struct student s;
printf(“Enter the data of student”);
scanf(“%d%d%s”,&s.roll,&s.age,s.name);
printf(“\nThe data of student is:\n”);
printf(“Roll No:%d\nAge:%d\nName:%s”,s.roll,s.age,s.name);
getch();
}
6/30/2024
Array of Structure

6/30/2024
Array of Structure
Syntax:
struct structure-name variable-name[size];
Example:
struct student s[100];

6/30/2024
Program to create a structure that read the information 100
students from the keyboard and print the same on the screen.
#include<stdio.h>
#include<conio.h>
struct student
{
int roll;
int age;
char name[20];
};
void main()
{
struct student s[100];
int i;
for(i=0;i<100;i++)
{
printf(“Enter the data of %d student”,i+1);
scanf(“%d%d%s”,&s[i].roll,&s[i].age,s[i].name);
}
for(i=0;i<100;i++)
{
printf(“\nThe data of %d student is:\n”,i+1);
printf(“Roll No:%d\nAge:%d\nName:%s”,s[i].roll,s[i].age,s[i].name);
}
getch();
}

6/30/2024
Nested Structure

6/30/2024
Nested Structure
• Structure that contain Example:
a structure variable as struct marks
its member
{
float m1;
float m2;
float m3;
};
struct student
{
int rollno;
char name[20];
struct marks m;
};
Nested Structure
Union
Union
• Collection of different types of data
• union keyword is used

6/30/2024
Defining Union
Syntax: Example:
union union-name union student
{ {
data-type member-1; int rollno;
data-type member-2; char name[20];
data-type member-3; float marks;
}; };
union: union program will be same as that of
structures. only keyword will defer.
structure vs union
Similarities-
1. Both can store different types of values.
2. User can define.
Difference-
1. The size of the structure variable is the sum of the sizes of its
members and in case of union its size of its largest member.
struct my_struct union my_union
{ {
double a; double a;
int b; int b;
char c[4]; char c[4];
}; };
sizeof(my_struct) = 8 + 4 + 4*1 = 16 sizeof(my_union) = max_of(8 , 4,
4*1) = 8
Dynamic Memory Allocation

6/30/2024
Allocation of Memory

• Static Allocation: Allocation of memory space


at compile time.

• Dynamic Allocation: Allocation of memory


space at run time.

6/30/2024
Difference between static memory allocation
and dynamic memory allocation

S.N. Static memory allocation Dynamic memory allocation


1. Memory is allocated during
Memory is allocated at run time.
compilation.
2. Faster execution than Dynamic. Slower execution than static.
3. Memory size can’t be modified Memory size can be modified
while execution. while execution.

4. Example: array Example: Linked list

6/30/2024
Dynamic Memory Allocation
• In dynamic memory allocation, the memory is
allocated at run time by using following
functions.
• malloc()
• calloc()
• realloc()
• free()

6/30/2024
Dynamic Memory Allocation Functions
• malloc ()
malloc (n *sizeof(int));

• malloc () function allocates a block of space in


memory.
• malloc () does not initialize the memory allocated
during execution. It carries garbage value.

6/30/2024
Dynamic Memory Allocation Functions
• calloc ()
calloc (n , sizeof(int));

• calloc () function allocates n blocks, each of


specified size in memory.
• calloc () initializes each block to zero.

6/30/2024
Dynamic Memory Allocation Functions
• realloc ()

realloc (pointer_name, size);

• realloc () function modifies the memory size


previously occupied by using malloc () or calloc ().

6/30/2024
Dynamic Memory Allocation Functions
• free ()
free (ptr);

• free () function frees the memory allocated by


malloc (), calloc (), realloc () functions.

6/30/2024
Difference between
malloc() and calloc()

S.N. malloc( ) calloc( )

1 It allocates only single block of It allocates multiple blocks of


requested memory requested memory
2 malloc () doesn’t initializes the calloc () initializes the allocated
allocated memory. It contains memory to zero
garbage values

6/30/2024
• Write a program to create an array using
dynamic memory allocation techniques.

6/30/2024
#include <stdio.h> printf("Enter elements of array: ");
void main() for(i=0;i<n;++i)
{ {
int n,i,*ptr; scanf("%d",ptr+i);
printf("Enter number of elements: "); }
scanf("%d",&n); printf("Enter elements of array: ");
ptr=(int*)malloc(n*sizeof(int)); for(i=0;i<n;++i)
if(ptr==NULL) {
{ printf("%d",*(ptr+i));
printf("Error! memory not }
allocated."); free(ptr);
exit(0); getch();
} }

6/30/2024
6/30/2024
Enumerated Data Type

6/30/2024
Enumerated Data Type
• User Defined Data Type
• enum keyword is used

6/30/2024
Defining Enumerated Data Type
Syntax:
enum identifier{value-1,value-2……….value-n};
Example:
enum day{Sun, Mon, Tue, Wed, Thu, Fri, Sat};

6/30/2024
Declaring Enumerated Type Variables
Syntax:
enum identifier var1, var2, var3;
Example:
enum day d1,d2;

6/30/2024
QUIZ

6/30/2024
Quiz
• Which keyword is used to define structure?
a. Structure
b. STRUCT
c. struct
d. None of these
Ans: c
• Which operator is used for accessing the members of a
structure?
a. *
b. .
c. &
d. None of these
Ans: b

6/30/2024
Quiz
• Find the error in the following structure
definition.
structure student
{
int roll; struct
char name[20];
float marks;
} ;

6/30/2024
FAQ’s

6/30/2024
FAQ’s
• What is meant by the following terms?
a. Nested Structures
b. array of structures
[MTU 2012-13 (Even) Marks-5] [MTU 2012-13 (Odd) Marks-2]
• Declare a structure that contains the following
information: Roll no., Name, Father's Name, Age,
City and Marks. Write a program in C to list all the
students who scored more than 75 marks.
[UPTU 2008-09 (Odd) Marks-10]
• What is the purpose of using structure in C?
Explain with the help of a suitable example.
[UPTU 2009-10 (Odd) Marks-5]

6/30/2024
FAQ’s
• Write a simple database program in C that stores the
personal details such as name, Date Of Birth, Address,
and Phone no. for 100 persons.[UPTU 2009-10 (Odd) Marks-
10]
• What is structure? Write a program to store the
records in hotel as Customer Name, Address, Period of
Stay, Room allotted and Room Charges.[GBTU 2011-12
(Even) Marks-5]
• Write a program in C that accepts the Roll No. and
Name of 60 students in class along with their marks in
Physics, Chemistry and mathematics. Print the Roll No.
and name of top 10 students in the order of merit. The
merit should be based on the sum of marks obtained in
three subjects. [GBTU 2012-13 (Odd) Marks-10]

6/30/2024
FAQ’s
• Define a structure called 'cricket' that will describe the following
information:
Player Name, Team Name, Batting Average
Using 'cricket', declare an array 'player' with 50 elements and write a
program to read the information about all the 50 players and print a
team-wise list containing names of players with their batting averages.
[MTU 2011-12 (Even) Marks-10]
• Describe a structure. Differentiate between a structure and an
array. Define a structure data type called time_struct containing
three members: integer hour, integer minute and integer second.
Write a program in C that would assign value to the individual
members and display the time in the following form:- 16:40:52
[MTU 2012-13 (Odd) Marks-10]

6/30/2024
FAQ’s
• List the differences between 'structure' and
'union'. [GBTU 2012-13 (Odd) Marks-2]

• What do you understand by enumerated data


type in C? Write a program to create
enumerated data type of seven days and
display their values in integer constant.
[UPTU 2010 (Carry) Marks-5]

6/30/2024

You might also like