Professional Documents
Culture Documents
Lecture 4 Structures in C++
Lecture 4 Structures in C++
Lecture 4 Structures in C++
Lecture 4
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. Pointers and Structures
8. Structures and Functions
Concept of a Structure
• We have already dealt with arrays. Arrays are used to store similar
type of data. Have you ever thought if there is any way to
store dissimilar data?
• For example, you are a student. Your name is a string and your
phone number and reg_no are integers. So, here name, address and
phone number are those different types of data. Here, structure
comes in the picture.
Concept of a Structure (2)
• Recall that elements of arrays must all be of the same type
scores
scores: : 85
85 79
79 92
92 57
57 68
68 80
80 . .. .. .
0 1 2 3 4 5 98 99
employee
employee R.
R.Jones
Jones 123
123Elm
Elm 6/12/55
6/12/55 $14.75
$14.75
4
Structure Definition
• Recall that an array is a collection of data items, all having the SAME
DATA TYPE and accessed using a common name and an INTEGER INDEX
into the collection.
• 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.
Structure Definition (2)
• A structure is a user-defined data type in C++. A structure creates a data
type that can be used to group items of possibly different types into a
single type.
• A structure is a collection of related data items stored in one place and can
be referenced by more than one names. Usually these data items are
different basic data types. Therefore, the number of bytes required to
store them may also vary.
• 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;
};
How to create a structure?(2)
• For example, to store and process • Here, struct is a keyword that
a student’s record with the tells the compiler that a
elements id_num , name and age, structure template is being
we can declare the following declared and student is a tag
structure. that identifies its data
structure.
struct student { • Tag is not a variable; it is a label
string id_num; for the structure’s template.
string name;
int age; • Note that there is a semicolon
}; after the closing curly brace.
How to create a structure?(3)
• A structure tags simply a label for the structure’s template but you
name the structure tag using the same rules for naming variables
• 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 student studno_1, studno_2;
How to create a structure?
|Structure Variables(2)
• In the above two cases, two structure variables,
studno_1 and studno_2, are declared
struct Point {
int x = 0; // COMPILER ERROR: cannot initialize members here
int y = 0; // COMPILER ERROR: cannot initialize members here
};
How to initialize structure members?(2)
• Structure members can be initialized using curly braces ‘{}’. For
example, the following is a valid initialization.
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};
}
#include <iostream>
using namespace std; How to access
struct Point {
structure elements?
int x, y;
};
• Structure members are
accessed using the dot (.)
int main() { operator.
struct Point p1 = { 0, 1 };
return 0;
} //Output: x = 20, y = 1
Structure Example(2)| declaration, initialization and
accessing structure elements
Structure Example(3)
| Copying Structure Elements
• We can also copy two
structures at one go. We
just have to write p1 = p2
and that's it. By writing
this, all the elements of
p1 will get copied to p2.
Array of Structures
int main() { cout << arr[0].x << " " << arr[0].y;
// Create an array of structures return 0;
struct Point arr[10]; }
• Output: 10 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;
}
• Like primitive types, we can have a
pointer to a structure. If we have a Pointers to Structures (2)
pointer to structure, members are
accessed using arrow ( -> ) operator
instead of the dot (.) operator.
// p2 is a pointer to structure p1
struct Point* p2 = &p1;
#include <iostream>
using namespace std; // Accessing structure members using
structure pointer
struct Point {
int x, y;
}; cout << p2->x << " " << p2->y;
return 0;
int main() { }
struct Point p1 = { 1, 2 }; Output: 1 2
Pointers to Structures(3)
Pointers to Structures(4)
• 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.
Structure to Function| Passing by Reference
• In passing by reference, the address of a structure variable is
passed to a function.
Structure to Function| Passing by Reference(2)
• 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.