You are on page 1of 27

UNIT-5

STRUCTURES
SYLLABUS
 Introduction to structures – Declaration –
Initialization – Accessing the members –
Nested Structures – Array of Structures –
Structures and functions – Passing an entire
structure
Exercise programs: Compute the age of a
person using structure and functions(passing
a structure to a function) Compute the
number of days an employee came late to
the office by considering his arrival time
for30 days (Use array of structures and
functions)
WHAT IS STRUCTURE?
 Structures (also called struct) is a collection
of variables (can be of different types) under
a single name.
 Each variable in the structure is known as
a member of the structure.
DECLARE/CREATE A STRUCTURE
 You can create a structure by using the struct keyword
and declare each of its members inside curly braces:
struct MyStructure
{
// Structure declaration
int myNum; // Member (int variable)
char myLetter; // Member (char variable)
}; // End the structure with a semicolon

 The structure definition does not allocates any


memory. It just gives a template that conveys to the C
compiler how the structure is laid out in memory and
gives details of the member names.
ACCESS A STRUCTURE
 Memory is allocated for the structure when we declare a
variable of the structure.
 To access the structure, you must create a variable of it.
 Use the struct keyword inside the main(), followed by the
name of the structure and then the name of the structure
variable:
 Create a struct variable with the name "s1":
struct myStructure
{
int myNum;
char myLetter;
};

void main()
{
struct myStructure s1;
}
ACCESS STRUCTURE MEMBERS
 To access members of a structure, use the dot (.)
struct myStructure {
int myNum;
char myLetter;
};
void main() {
struct myStructure s1;
s1.myNum = 31;
s1.myLetter = ‘S';
printf("My number: %d\n", s1.myNum);
printf("My letter: %c\n", s1.myLetter);
}
Output
My number: 31
My letter: S
WAY1
struct Student {
char name[50];
int class;
int roll_no;
};
int main()
{
struct Student stud1;
}
WAY2
Syntax:
struct structure_name {
// body of structure
} variables;
Eg:
struct Student {
char name[50];
int class;
int roll_no;
} stud1; // here 'student1' is a structure
variable of type, Student.
 you can easily create multiple structure variables with
different values, using just one structure:
struct myStructure {
int myNum;
char myLetter;
};
void main() {
struct myStructure s1;
struct myStructure s2;
s1.myNum = 31;
s1.myLetter = ‘P';
s2.myNum = 24;
s2.myLetter = ‘S';
// Print values
printf("s1 number: %d\n", s1.myNum);
printf("s1 letter: %c\n", s1.myLetter);
printf("s2 number: %d\n", s2.myNum);
printf("s2 letter: %c\n", s2.myLetter);
}
INITIALIZATION OF STRUCTURE
 Initializing a structure member means assigning values to the
structure members according to their respective data types.
 The general syntax to initialize a structure variable is given
as follows.
Struct struct_name
{data_typemember_name1;
data_typemember_name2;
.......................................
}struct_var= {constant1, constant2, constant 3,...};
OR
Struct struct_name
{data_typemember_name1;
data_typemember_name2;
.......................................
};
Struct struct_name struct_var= {constant1, constant2, ….};
 structure members cannot be initialized
during the declaration
struct Student {
char name[50] = {"Student1"}; // COMPILER
ERROR: cannot initialize members here
int class = 1; // COMPILER ERROR: cannot
initialize members here
int roll_no = 5; // COMPILER ERROR: cannot
initialize members here
};
 there are two ways to initialize structure
members:
 1.Using dot '.' operator
 Using the dot (.) operator, we can access any
structure member and then initialize or assign its
value according to its data type.
 Syntax
 struct structure_name variable_name;
variable_name.member = value;
 In the above syntax first, we created a structure
variable, then with the help of the dot operator
accessed its member to initialize them.
#include <stdio.h>
#include <string.h>
struct Student {
char name[50];
int class;
char section;
};
void main() {
Struct Student student1;
strcpy(student1.name,"Student_name");
student1.class = 1;
student1.section = 'A';
printf( "Student Name : %s\n", student1.name);
printf( "Student Class : %d\n", student1.class);
printf( "Student Section : %c\n", student1.section);
}
 2.Using curly braces ‘{}’
 If we want to initialize all the members
during the structure variable declaration, we
can declare using curly braces.
 syntax
 struct stucture_name v1 = {value, value,
value, ..};
 To initialize the data members by this method,
the comma-separated values should be provided
in the same order as the members declared in
the structure.
#include <stdio.h>
#include <string.h>
struct Student {
char name[50];
int class;
char section;
};
int main()
{
Student struct Student student1 = {"Student_name" , 1,
'A'};
printf( "Student Name : %s\n", student1.name);
printf( "Student Class : %d\n", student1.class);
printf( "Student Section : %c\n", student1.section);
}
STRING IN STRUCTURES
 #include <stdio.h>
struct myStructure {
int myNum;
char myLetter;
char myString[30]; // String
};
void main() {
struct myStructure s1;
s1.myString = "Some text";
printf("My string: %s", s1.myString);
}
output
error: assignment to expression with array type
SOLUTION
 You can use the strcpy() function and assign the
value to s1.myString
struct myStructure {
int myNum;
char myLetter;
char myString[30]; // String
};
void main() {
struct myStructure s1;
strcpy(s1.myString, “I Love Cricket");
printf("My string: %s", s1.myString);
}
Result:
My string: I Love Cricket
COPY STRUCTURE
 You can also assign one structure to another.
 In the following example, the values of s1 are copied to s2:

#include <stdio.h>
struct myStructure {
int myNum;
char myLetter;
char myString[30];
};
void main() {
struct myStructure s1 = {13, 'B', "Some text"};
struct myStructure s2;
s2 = s1;
printf("%d %c %s", s2.myNum, s2.myLetter, s2.myString);
}
NESTED STRUCTURE
 A structure inside another structure is called
nested structure.
 In C Nested Structure can be defined in two
ways:
 By separate structure
 By Embedded structure
1.EMBEDDED STRUCTURE
 This method is used to embed one structure inside
another, i.e., defining one structure in the definition
of another structure. Thus using this method, the
nested structure will be declared inside the parent
structure.
struct Parent{
data member 1;
data member 2;

struct NestedStructure{
data member 3;
data member 4;
}X;
};
 struct Employee
 {
 int id;
 char name[20];
 struct Date
 {
 int dd;
 int mm;
 int yyyy;
 }doj;
 }emp1;
 #include <stdio.h>
 #include <string.h>
 struct Employee
 {
 int id;
 char name[20];
 struct Date
 {
 int dd;
 int mm;
 int yyyy;
 }doj;
 }e1;
 int main( )
 {
 //storing employee information
 e1.id=101;
 strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array
 e1.doj.dd=10;
 e1.doj.mm=11;
 e1.doj.yyyy=2014;

 //printing first employee information


 printf( "employee id : %d\n", e1.id);
 printf( "employee name : %s\n", e1.name);
 printf( "employee date of joining (dd/mm/yyyy) : %d/%d/%d\n", e1.doj.dd,e1.doj.mm,e1.doj.yyyy);
 return 0;
 }
SEPARATE STRUCTURE
 Here, we create two structures, but the
dependent structure should be used inside
the main structure as a member. Consider
the following example.
struct Date
{
int dd;
int mm;
int yyyy;
};
struct Employee
{
int id;
char name[20];
struct Date doj;
}emp1;
 As you can see, doj (date of joining) is the
variable of type Date. Here doj is used as a
member in Employee structure. In this way, we
can use Date structure in many structures.
 As you can see, doj (date of joining) is the
variable of type Date. Here doj is used as a
member in Employee structure. In this way,
we can use Date structure in many
structures.
ACCESSING NESTED STRUCTURE
 We can access the member of the nested
structure by
Outer_Structure.Nested_Structure.member
 #include <stdio.h>
 #include <string.h>
 struct Employee
 {
 int id;
 char name[20];
 struct Date
 {
 int dd;
 int mm;
 int yyyy;
 }doj;
 }e1;
 int main( )
 {
 //storing employee information
 e1.id=101;
 strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array
 e1.doj.dd=10;
 e1.doj.mm=11;
 e1.doj.yyyy=2014;

 //printing first employee information
 printf( "employee id : %d\n", e1.id);
 printf( "employee name : %s\n", e1.name);
 printf( "employee date of joining (dd/mm/yyyy) : %d/%d/%d\
n", e1.doj.dd,e1.doj.mm,e1.doj.yyyy);
 return 0;
 }

You might also like