0% found this document useful (0 votes)
34 views22 pages

PPS - Unit 8

The document discusses structures in C programming. It defines a structure as a collection of logically related data of different data types grouped together under a single name. Some key points: - Structures allow combining different data types like integers, floats, chars etc to represent real-world entities. - Structure variables are declared to access member elements using dot (.) and arrow (->) operators. - Arrays of structures can be used to represent multiple structures. - Structure pointers can also be used to access member elements through dereferencing the pointer. - Examples are provided to demonstrate declaring structures, defining structure variables, reading/writing members, and using arrays and pointers with structures.

Uploaded by

raaharpalsinh1
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)
34 views22 pages

PPS - Unit 8

The document discusses structures in C programming. It defines a structure as a collection of logically related data of different data types grouped together under a single name. Some key points: - Structures allow combining different data types like integers, floats, chars etc to represent real-world entities. - Structure variables are declared to access member elements using dot (.) and arrow (->) operators. - Arrays of structures can be used to represent multiple structures. - Structure pointers can also be used to access member elements through dereferencing the pointer. - Examples are provided to demonstrate declaring structures, defining structure variables, reading/writing members, and using arrays and pointers with structures.

Uploaded by

raaharpalsinh1
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/ 22

PPS - Unit 8

Structure
Syllabus Topics

✓ Basics of structure

✓ structure members

✓ accessing structure members

✓ nested structures

✓ array of structures

✓ structure and functions

✓ structures and pointers


Data Types
✓ Data types are describe the type of the value a variable can hold.

Data types in C

Primary Data type


Secondary Data type
(int, char, float, double)

Derived Data type User defined Data type


(array, pointer) (structure, union, enum)
Data Types

✓ 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
Structure Concept
▪ “Structure is a collection of logically related data of different datatypes grouped
together under single name”.

▪ It is a group of elements of different type.

▪ Structure is a user defined datatype.

▪ Structure helps to build a complex datatype which is more meaningful than an array.

▪ An array holds elements of similar datatype, while structure holds elements of different
datatypes.

▪ Two fundamental aspects of Structure:

▪ Declaration of Structure Variable

▪ Accessing of Structure Member


Structure Syntax
• 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.

• Members can be normal variables, pointers, arrays or other structures.

Syntax

struct structure_name Structure Tag


{
member1_declaration;
member2_declaration;
. . . Structure Member Declaration
memberN_declaration;
};
Structure Example
Example

struct student
{
char name[30];
int roll_no;
float CPI;
int backlog;
};

• You must terminate structure definition with semicolon ;.

• You cannot assign value to members inside the structure definition, it will cause
compilation error.
Create Structure Variable

• A data type defines various properties about data stored in memory.

• To use any type we must declare its variable.

• Hence, let us learn how to create our custom structure type objects also known
as structure variable.

• In C programming, there are two ways to declare a structure variable:

1) Along with structure definition

2) After structure definition


Create Structure Variable

1) Declaration along with the structure definition

Syntax Example
struct structure_name struct student
{ {
member1_declaration; char name[30];
member2_declaration; int roll_no;
. . . float CPI;
memberN_declaration; int backlog;

} structure_variable; } student1;
Create Structure Variable
2) Declaration after Structure definition

Syntax
struct StructureName StructureVariable;

Example
struct student
{
char name[30];
int roll_no;
float CPI;
int backlog;
};

struct student student1;


Access Structure Member

• 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)

2)
#include<stdio.h>

struct Student
{ Write a program to read and display
char Name[20]; student information using structure.
int Roll_no;
float Marks;
}S1;

void main()
{
printf(" Enter Name : ");
scanf("%s", S1.Name);

printf(" Enter Roll_no : ");


Output
scanf("%d", &S1.Roll_no );
Enter Name : Aakash
printf(" Enter Marks : "); Enter Roll_no : 11
scanf("%f", &S1.Marks ); Enter Marks : 95

printf("\n --------- Student Details --------- \n"); --------- Student Details ---------

printf("\n Student Name = %s ",S1.Name); Student Name = Aakash


printf("\n Student Roll_No = %d ",S1.Roll_no); Student Roll_No = 11
printf("\n Student marks = %f ",S1.Marks); Student marks = 95.000000
}
Array of Structure

• “collection of multiple structure variables” is known as array of structure.

Syntax Example
struct student
struct structure_name {
{ char name[30];
member1_declaration; int roll_no;
member2_declaration; float CPI;
. . . int backlog;
memberN_declaration;
} S[5];
} structure_variable[size];
#include<stdio.h>
struct Student
{
char Name[20]; Write a program
int Roll_no;
float Marks;
to read and
}S[3]; display student
information for 3
void main() students using
{ int i;
array of
for(i=0;i<3;i++) structure.
{
printf("\n ---------- Enter Details of Student %d ---------- \n",i+1);

printf(" Enter Name : ");


scanf("%s",S[i].Name);

printf(" Enter Roll_no : ");


scanf("%d",&S[i].Roll_no );

printf(" Enter Marks : ");


scanf("%f", &S[i].Marks );
}
printf("\n ------------ All Students Detail ------------ \n");
printf("\n Student Name | Student Roll No. | Student Marks \n");
for(i=0;i<3;i++)
{
printf("\n %s \t %d \t\t %f \n",S[i].Name,S[i].Roll_no,S[i].Marks);
}
}
Output

---------- Enter Details of Student 1 ----------


Enter Name : Yash
Enter Roll_no : 1
Enter Marks : 99

---------- Enter Details of Student 2 ----------


Enter Name : Prisha
Enter Roll_no : 2
Enter Marks : 95

---------- Enter Details of Student 3 ----------


Enter Name : Aarav
Enter Roll_no : 3
Enter Marks : 97

------------ All Students Detail ------------

Student Name | Student Roll No. | Student Marks

Yash 1 99.000000
Prisha 2 95.000000
Aarav 3 97.000000
Structure and pointers
• Address of structure variable is stored in a pointer of Structure type.

• Using -> operator, we can access structure members using structure pointer.

Syntax Example
struct student
struct structure_name {
{ char name[30];
member1_declaration; int roll_no;
member2_declaration; float CPI;
. . . int backlog;
memberN_declaration;
} S, *p;
} Structure_Variable, *Pointer_Name;
Program
#include<stdio.h>
struct Student Write a program to access
{ Structure Members using a
char Name[20]; Structure Pointer
int Roll_no;
float Marks;

}S1,*p;
void main()
{
p = &S1;
printf(" Enter Name : ");
scanf("%s",p->Name);
printf(" Enter Roll_no : ");
scanf("%d",&p->Roll_no ); Output
printf(" Enter Marks : "); Enter Name : Smit
scanf("%f", &p->Marks ); Enter Roll_no : 1
Enter Marks : 65
printf("\n --------- Student Details --------- \n");
--------- Student Details ---------
printf("\n Student Name = %s ",p->Name);
printf("\n Student Roll_No = %d ",p->Roll_no); Student Name = Smit
printf("\n Student marks = %f ",p->Marks); Student Roll_No = 1
} Student marks = 65.000000
Nested Structures

• A structure within a structure is known as nested structure.


Syntax

struct structure_name1
{
Example
member1_declaration;
member2_declaration; struct student
. . . {
memberN_declaration; char name[30];
int roll_no;
struct structure_name2 float CPI;
{ int backlog;
// member_declaration;
} S, *p;
} Inner_Structure_Variable;

} Outer_Structure_Variable;
Union
▪ The concept of union is borrowed from structure, but both differ in the terms of
memory storage.

▪ In structure each member has its own storage,

▪ while in union all the members share same storage.

▪ This means that in union at a time only one member can be accessed.

▪ The compiler allocates the storage that is large enough to hold the largest data type
among the union members.
Enumeration
▪ Enum is a keyword.
enum Weekdays
▪ “It is used to define more than one
{
integer symbolic constants”. Sunday,
▪ Compiler automatically assigns integer Monday,
digits beginning with 0 and remaining Tuesday,

values are assigned as 1,2,3 and so on. Wednesday,


Thursday,
Friday,
Saturday
};
THANK YOU

You might also like