19CSE102
Computer Programming
Structure in C
1
List of Topics
• Basics of Structure
• Array of Structure
• Nested Structure
• Pointer to Structure
2
Introduction to Structure
• Array : Represent a group of data items that belongs
to the same class.
• Question : How to represent collection of data items
of different types that are logically related to a
particular entity?
Structure
3
Syntax
struct structure_name{
data_type var1;
data_type var2;
data_type var3;
...
};
Structure : Collection of different types of variables under single
name
4
Defining a Structure
• Once structure_name is declared as new data type,
then variables of that type can be declared as:
struct structure_name structure_variable;
Structure is a conveient tool for handling a group of
logically related data items
5
Example
Structure_name Members
Time Seconds, minutes, hours
date Day, month, year
Book Author, title, price, year
address name, door_number, street, city
Person Name, gender, age, address,
aadhar_number
6
Example Structure-1
struct person
{
char name[30]; struct person person1,person2;
char gender;
int age;
char address[][60];
int aadhar_number;
};
7
Example Structure-2
struct student
{
char name[20]; struct student st1, st2, st3;
int roll_no;
float marks;
char gender;
long int phone_no;
};
8
Structure
• Each variable of structure has its own copy of member
variables.
• The member variables are accessed using the dot (.)
operator or member operator.
For example:
st1.name is member variable name of st1 structure variable
st3.gender is member variable gender of st3 structure
variable.
9
Structure initialization
• Syntax: struct structure_name
structure_variable={value1, value2, … , valueN};
• There is a one-to-one correspondence between the
members and their initializing values.
• Note: C does not allow the initialization of individual
structure members within the structure definition
template.
10
Sample program
#include <stdio.h>
struct student
Sample output :
{
char name[20]; Name Roll No. Marks Gender PhoneNo
int roll_no; .........................................................................
float marks;
char gender; Amrita 4 89.000000 F 944445555
long int phone_no;
};
int main()
{
struct student st1={"Amrita", 4, 89,‘F’,944445555 };
printf("Name\t\t\tRoll No.\tMarks\t\tGender\tPhone No.");
printf("\n.........................................................................\n");
printf("\n %s\t\t %d\t\t %f\t %c\t %ld", st1.name, st1.roll_no, st1.marks, st1.gender, st1.phone_no);
return 0;
}
11
Partial Initialization
• We can initialize the first few members and leave the
remaining blank.
• However, the uninitialized members should be only
at the end of the list.
• The uninitialized members are assigned default
values as follows:
– Zero for integer and floating point numbers.
– ‘\0’ for characters and strings.
12
Sample program-2
#include <stdio.h>
struct student
{ char name[20];
int roll_no;
float marks;
char gender;
long int phone_no;
};
int main()
{
struct student st1={"Amrita", 4 };
st1.marks=89;
scanf(" %c",&st1.gender);
scanf("%ld",&st1.phone_no);
("Name\t\t\tRoll No.\tMarks\t\tGender\tPhone No.");
printf("\n.........................................................................\n");
printf("\n %s\t\t %d\t\t %f\t %c\t %ld", st1.name, st1.roll_no, st1.marks, st1.gender, st1.phone_no);
return 0;
}
13
Question
#include <stdio.h>
int main()
{
struct simp
{
int i;
char city[15]; What will be the output of c program?
};
struct simp s1={6,"chennai"};
printf("%s \t",s1.city);
printf("%d",s1.i);
return 0;
}
14
Practice Programs
• Create a structure named student that has name, roll and mark in 5 subjects as
members. Write a program using structure to read and display the data
entered by the user.
• Define a structure data type called time_struct containing three members
hour, minute and second. Develop a program that would assign values to the
individual member and display the time in the following format:
16:40:51
• Modify the above program such that a function is used to input values to the
members and another function to display time.
• Create a structure to add and subtract two complex numbers using structures.
15
References
• Byron Gottfried. Programming With C. Fourth Edition,
McGrawHill,; 2018.
• Brian W. Kernighan and Dennis M. Ritchie. The C Programming
Language. Second Edition, Prentice Hall, 1988.
• Eric S. Roberts. Art and Science of C. Addison Wesley; 1995.
• Jeri Hanly and Elliot Koffman. Problem Solving and Program
Design in C. Fifth Edition, Addison Wesley (Pearson); 2007.
• Balagurusamy,Computing Fundamentals & C Programming,
Tata McGraw-Hill,2008
16
Thank you ……
17