Struct is a user-defined data type that includes many elements that can be of many different data types. The items in the structure are called its member and they can be of any valid data type.
Declaring Struct
Syntax:
data_type member_name1;
data_type member_name2;
. . . .
. . . .
};
Note:The above syntax is also called a structure template or structure prototype and no memory is allocated to the structure in the declaration.
1 2 3 4 5 | struct Student { char name[20]; int id; int age; }; |
Access Structure Members
Syntax:
1 2 3 4 5 6 7 8 9 10 11 12 | struct wrapline { int myNum; string myString; }; // Assign values to members of myStructure wrapline.myNum = 1; wrapline.myString = "Welcome to Wrapline!"; // Print members of myStructure cout << wrapline.myNum << "\n"; cout << wrapline.myString << "\n"; |
Output:
1 2 | 1 Welcome to Wrapline! |
Initialize Structure Members
Structure members cannot be initialized with the declaration. For example, the following C program fails in the compilation.
1 2 3 4 5 | struct Point { int x = 0; // COMPILER ERROR: cannot initialize members here int y = 0; // COMPILER ERROR: cannot initialize members here }; |
The reason for the above error is simple. When a datatype is declared, no memory is allocated for it. Memory is allocated only when variables are created.
We can initialize structure members in 3 ways which are as follows:
- Using Assignment Operator.
- Using Initializer List.
Initialization using Assignment Operator
Syntax:
str.member1 = value1;
str.member2 = value2;
str.member3 = value3;
.
.
.
Initialization using Initializer List
Syntax:
In this type of initialization, the values are assigned in sequential order as they are declared in the structure template.
typedef for Structures
The typedef keyword is used to define an alias for the already existing datatype.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include <stdio.h> // defining structure struct str1 { int a; }; // defining new name for str1 typedef struct str1 str1; // another way of using typedef with structures typedef struct str2 { int x; } str2; int main() { // creating structure variables using new names str1 var1 = { 20 }; str2 var2 = { 314 }; printf("var1.a = %d\n", var1.a); printf("var2.x = %d", var2.x); return 0; } |
Output:
1 2 | var1.a = 20 var2.x = 314 |
An Array of Structures
Like other primitive data types, we can create an array of structures.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <iostream> using namespace std; struct Point { int x, y; }; int main() { // Create an array of structures struct Point arr[10]; // Access array members arr[0].x = 10; arr[0].y = 20; cout << arr[0].x << " " << arr[0].y; return 0; } |
Output:
1 | 10 20 |
Nested Structs
Nested structures are structures that contain another structure. Nested struct types are rarely used in programs.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include <iostream> using namespace std; struct Employee { short id; int age; double wage; }; struct Company { Employee CEO; int numberOfEmployees; }; Company myCompany{{ 1, 42, 60000.0 }, 5 }; int main(){ cout << "id: " << myCompany.CEO.id << endl; cout << "age: " << myCompany.CEO.age << endl; cout << "wage: " << myCompany.CEO.wage << endl; cout << "numberOfEmployees: " << myCompany.numberOfEmployees << endl; } |
Output:
1 2 3 4 | id: 1 age: 42 wage: 60000 numberOfEmployees: 5 |
Structure Pointer
The syntax for declaring pointers is as follows:
To access elements in the struct with pointers, we can use one of the following two ways:
or
(*pointer_name). element_name;
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <iostream> using namespace std; struct Point { int x, y; }; int main() { struct Point p1 = { 1, 2 }; // p2 is a pointer to structure p1 struct Point* p2 = &p1; // Accessing structure members using // structure pointer cout << p2->x << " " << p2->y; return 0; } |
Output:
1 | 1 2 |