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

Unit 7 Structure

The document provides an overview of data types in C, focusing on user-defined data types such as structures. It explains the definition, syntax, and usage of structures, including how to create structure variables and access their members. Additionally, it covers concepts like arrays of structures and nested structures, with examples demonstrating how to read and display student information.

Uploaded by

anjalivp1704
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)
2 views16 pages

Unit 7 Structure

The document provides an overview of data types in C, focusing on user-defined data types such as structures. It explains the definition, syntax, and usage of structures, including how to create structure variables and access their members. Additionally, it covers concepts like arrays of structures and nested structures, with examples demonstrating how to read and display student information.

Uploaded by

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

UNIT-7 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.
▪ We need custom datatype for different situation.
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 variable
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;
float cpi; Enter Name: viral
}; Enter RollNo: 111
int main()
{ Enter CPI: 8
struct student *studPtr, stud1;
studPtr = &stud1;
printf("Enter Name: ");
Student Details:
scanf("%s", studPtr->name); Name: viral
printf("Enter RollNo: "); RollNo: 111
scanf("%d", &studPtr->rollno);
printf("Enter CPI: "); CPI: 8.000000
scanf("%f", &studPtr->cpi);
printf("\nStudent Details:\n");
printf("Name: %s\n", studPtr->name);
printf("RollNo: %d", studPtr->rollno);
printf(”\nCPI: %f", studPtr->cpi); Example::Structure2.c
return 0;
}
Example:
▪ Write a program to declare time structure and read two different
time period and display sum of it.(Structure_use.c)
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.(structure_array.c)
#include<stdio.h>
struct student { Output
char name[20];
Enter how many records u want to store
int rollno;
3
float cpi;
};
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);
Enter Name : hiren
printf("Enter CPI : ");
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 10.00
return 0;
}
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; Example::Structure_nested.c
};
struct structure_name2
{
member1_declaration;
member2_declaration;
...
struct structure1 obj;

You might also like