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

DSA With C Structure

Uploaded by

meetdave642
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)
15 views

DSA With C Structure

Uploaded by

meetdave642
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/ 16

Unit-8 Structure

1
Data Types
 Data types are defined as the data storage format that a variable can
store a data.
 It determines the type and size of data associated with variables.

Data types in C

Primary Data type Secondary Data


(int, float, char) type

Derived Data type User definer Data type


(array, pointer) (structure, union, enum)
)
 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.
User Defined Datatype
 We need combination of various datatypes to understand
different entity/object.
 Example::
What is Structure?
 Structure is a collection of logically related data items of different
datatypes grouped together under single name.
 Structure is a user defined datatype.
 Structure helps to build a complex datatype which is more
meaningful than an array.
 But, an array holds similar datatype record, when structure holds
different datatypes records.
 Two fundamental aspects of Structure:
• Declaration of Structure Variable
• Accessing of Structure Member
Syntax to Define Structure
 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.
Syntax
struct structure_name structure_name is name of
{ custom type
member1_declaration;
member2_declaration; memberN_declaration is
. . . individual memberdeclaration
memberN_declaration;
};

 Members can be normal variables, pointers, arrays or other


structures.
 Member names within the particular structure must be distinct
from one another.
Example to Define Structure
Example
struct student
{
char name[30]; // Student Name
int roll_no; // Student Roll No
float CPI; // Student CPI
int backlog; // Student Backlog
};
 You must terminate structure definition with semicolon ;.
 You cannot assign value to members inside the structure definition,
it will cause compilation error.

struct student
{
char name[30] = “ABC”; // Student Name
. . .
};
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 – Cont.
 Declaration along with the structure definition
struct structure_name struct student
{ {
member1_declaration; char name[30]; // Student Name
member2_declaration; int roll_no; // Student Roll No
. . . float CPI; // Student CPI
memberN_declaration; int backlog; // Student Backlog
} structure_variable; } student1;
 Declaration after Structure definition
struct structure_name structure_variable;

struct student
{
char name[30]; // Student Name
int roll_no; // Student Roll No
float CPI; // Student CPI
int backlog; // Student Backlog
};
struct student student1; // Declare structure
Access Structure member (data)
 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. Dot/period operator (.)
2. Arrow operator (->)
Access Structure member (data) – Cont.
1. Dot/period operator (.)
• It is known as member access operator. We use dot operator to access
members of simple structure variable.
Syntax Example
structure_variable.member_name; // Assign CPI of student1
student1.CPI = 7.46;
2. Arrow operator (->)
• In C language it is illegal to access a structure member from a pointer to
structure variable using dot operator.
• We use arrow operator to access structure member from pointer to
structure.

Syntax Example
pointer_to_structure->member_name; // Student1 is a pointer to
student type
student1 -> CPI = 7.46;
Write a program to read and display student information using structure.

#include <stdio.h>
struct student
{
char name[40]; // Student name Output
int roll; // Student enrollment
float CPI; // Student mobile number Enter Student Name:viral
int backlog; Enter Student Roll Number:111
}; Enter Student CPI:8
int main()
{
Enter Student Backlog:0
struct student student1; // Simple structure variable
// Input data in structure members using dot operator Student using simple structure
printf("Enter Student Name:"); variable.
scanf("%s", student1.name); Student name: viral
printf("Enter Student Roll Number:");
scanf("%d", &student1.roll); Student Enrollment: 111
printf("Enter Student CPI:"); Student CPI: 8.000000
scanf("%f", &student1.CPI); Student Backlog: 0
printf("Enter Student Backlog:");
scanf("%d", &student1.backlog);
// Display data in structure members using dot operator
printf("\nStudent using simple structure variable.\n");
printf("Student name: %s\n", student1.name); Example::Structure1.c
printf("Student Enrollment: %d\n", student1.roll);
printf("Student CPI: %f\n", student1.CPI);
printf("Student Backlog: %i\n", student1.backlog);
}
Structure using Pointer
#include <stdio.h>
struct student {
char name[20]; Output
int rollno; Enter Name: viral
float cpi; Enter RollNo: 111
}; Enter CPI: 8
int main()
{
struct student *studPtr, stud1;
Student Details:
studPtr = &stud1; Name: viral
printf("Enter Name: "); RollNo: 111
scanf("%s", studPtr->name); CPI: 8.000000
printf("Enter RollNo: ");
scanf("%d", &studPtr->rollno);
printf("Enter CPI: ");
scanf("%f", &studPtr->cpi);
printf("\nStudent Details:\n");
printf("Name: %s\n", studPtr-
>name);
printf("RollNo: %d", studPtr-
>rollno);
printf(”\nCPI: %f", studPtr->cpi);
return 0;
Array of Structure
 It can be defined as the collection of multiple structure variables
where each variable contains information about different entities.
 The array of structures in C are used to store information about
multiple entities of different data types.
Syntax
struct structure_name
{
member1_declaration;
member2_declaration;
...
memberN_declaration;
} structure_variable[size];
Write a program to read and display N student
information using array of structure
#include<stdio.h>
struct student { Output
char name[20];
Enter how many records u want to store
int rollno;
float cpi;
: 3
};
int main( ) { Enter 1 record :
int i,n; Enter Name : viral
printf("Enter how many records u want to store : "); Enter RollNo. : 111
scanf("%d",&n); Enter CPI : 8
struct student sarr[n];
for(i=0; i<n; i++) Enter 2 record :
{ Enter Name : anand
printf("\nEnter %d record : \n",i+1); Enter RollNo. : 222
printf("Enter Name : "); Enter CPI : 9
scanf("%s",sarr[i].name);
printf("Enter RollNo. : ");
Enter 3 record :
scanf("%d",&sarr[i].rollno);
printf("Enter CPI : ");
Enter Name : hiren
scanf("%f",&sarr[i].cpi); Enter RollNo. : 333
} Enter CPI : 10
printf("\n\tName\tRollNo\tMarks\t\n");
for(i=0; i<n; i++) { Name RollNo Marks
printf("\t%s\t\t%d\t\t%.2f\t\n", sarr[i].name, viral 111 8.00
sarr[i].rollno, sarr[i].cpi); anand 222 9.00
} hiren 333
return 0; 10.00
}
Nested Structure
 When a structure contains another structure, it is called nested structure.
 For example, we have two structures named Address and Student. To make
Address nested to Student, we have to define Address structure before and
outside Student structure and create an object of Address structure inside
Student structure. struct structure_name1
{
member1_declaration;
member2_declaration;
...
memberN_declaration;
};
struct structure_name2
{
member1_declaration;
member2_declaration;
...
struct structure1 obj;
};
Example:

struct point
{
int x, y;
};
struct Rectangle
{
struct point p1, p2, p3, p4;
};

You might also like