Chapter 3 - Structure
Chapter 3 - Structure
Chapter 3
Lecture Outline
1. Concept of a structure
2. Structure definition
3. How to declare structures
4. How to initialize structures
5. How to access structures
6. Arrays and Structures
7. Nested Structure
8. Pointers and Structures
9. Structures and Functions
10. Function Return Structure
• A struct is also a collection of data items, except with a struct the data
items can have DIFFERENT DATA TYPES, and the individual fields within
the struct are ACCESSED BY NAME instead of an integer index. These
data items are called MEMBERS
• Structs are very powerful for BUNDLING TOGETHER data items that
collectively describe a thing, or are in some other way related to each
other.
• The ‘struct’ keyword is used to create a structure. The general syntax to create a
structure is as shown below:
struct structureName{
data-type member1;
data-type member2;
.
.
.
data-type memberN;
};
• Compiler will not reserve memory for a structure until you declare
a structure variable same as you would declare normal variables
such as int or float.
struct Point {
int x, y;
};
int main() {
// A valid initialization. member x gets value 0 and y
// gets value 1. The order of declaration is followed.
struct Point p1 = {0, 1};
}
• Output: 10 20
Fundamentals of Prog. II | Structure 20
Pointers to Structures
• Like we have pointers to int, char and other data-types, we also have
pointers pointing to structures. These pointers are called structure pointers.
Below is h0w we define a structure pointer
• struct structure_name{
data-type member-1;
data-type member-2;
data-type member-N;
};
int main(){
struct structure_name *ptr;
}
• cout << ptr->name << ptr->roll_no << endl; - We use -> operator to
access the members of a structure using a pointer to that
structure.
• We first declared a structure named student with roll_no, name and phone
number as its members and 's' as its variable. Then we assigned the values of roll
number, name and phone number to the structure variable s. Just as we pass any
other variable to a function, we passed the structure variable 's' to a function
'display'.
• Now, while defining the function, we passed a copy of the variable 's' as its
argument with 'struct student' written before it because the variable which we
have passed is of type structure named student. Finally, in the function, we
printed the name, roll number and phone number of the structure variable.
• This case is similar to the previous one, the only difference is that
this time, we are passing the address of the structure variable to
the function.
• While declaring the function, we passed the pointer of the copy 'st'
of the structure variable 's' in its parameter. Since the pointer is of
a variable of type structure named student, we wrote 'struct
student' before the name of the pointer in the argument of the
function. In the function , we accessed the members of the pointer
using -> sign as discussed before.
Enter Employee Id : 10
Enter Employee Name : Ajay
Enter Employee Age : 25
Enter Employee Salary : 15000
Employee Id : 10
Employee Name : Ajay
Employee Age : 25
Employee Salary : 15000