Linux
Top 60 Linux Interview Questions and Answers (Download PDF)
We have organized the most frequently asked Linux Interview Questions and Answers that help...
A STRUCT is a C++ data structure that can be used to store together elements of different data types. In C++, a structure is a user-defined data type. The structure creates a data type for grouping items of different data types under a single data type.
For example:
Suppose you need to store information about someone, their name, citizenship, and age. You can create variables like name, citizenship, and age to store the data separately.
However, you may need to store information about many persons in the future. It means variables for different individuals will be created. For example, name1, citizenship1, age1 etc. To avoid this, it's better to create a struct.
In this C++ tutorial, you will learn:
Here are some reasons using structure in C++.
To create a C++ structure, we use the struct keyword, followed by an identifier. The identifier becomes the name of the struct. Here is the syntax for creation of a C++ struct:
Syntax:
struct struct_name
{
// struct members
}
In the above syntax, we have used the struct keyword. The struct_name is the name of the structure.
The struct members are added within curly braces. These members probably belong to different data types.
For example:
struct Person
{
char name[30];
int citizenship;
int age;
}
In the above example, Person is a structure with three members. The members include name, citizenship, and age. One member is of char data type, while the remaining 2 are integers when a structure is created, memory is not allocated. Memory is only allocated after a variable is added to the struct.
In the above example, we have created a struct named Person. We can create a struct variable as follows:
Person p;
The p is a struct variable of type Person. We can use this variable to access the members of the struct.
To access the struct members, we use the instance of the struct and the dot (.) operator. For example, to access the member age of struct Person:
p.age = 27;
We have accessed the member age of struct Person using the struct's instance, p. We have then set the value of the member age to 27.
#include <iostream>
using namespace std;
struct Person
{
int citizenship;
int age;
};
int main(void) {
struct Person p;
p.citizenship = 1;
p.age = 27;
cout << "Person citizenship: " << p.citizenship << endl;
cout << "Person age: " << p.age << endl;
return 0;
}
Output:
Here is a screenshot of the code:
Code Explanation:
It's possible to create a pointer that points to a structure. It is similar to how pointers pointing to native data types like int, float, double, etc. are created. Note that a pointer in C++ will store a memory location.
#include <iostream>
using namespace std;
struct Length
{
int meters;
float centimeters;
};
int main()
{
Length *ptr, l;
ptr = &l;
cout << "Enter meters: ";
cin >> (*ptr).meters;
cout << "Enter centimeters: ";
cin >> (*ptr).centimeters;
cout << "Length = " << (*ptr).meters << " meters " << (*ptr).centimeters << " centimeters";
return 0;
}
Output:
Here is a screenshot of the code:
Code Explanation:
You can pass a struct to a function as an argument. This is done in the same way as passing a normal argument. The struct variables can also be passed to a function. A good example is when you need to display the values of struct members. Let's demonstrates this:
#include<iostream>
using namespace std;
struct Person
{
int citizenship;
int age;
};
void func(struct Person p);
int main()
{
struct Person p;
p.citizenship = 1;
p.age = 27;
func(p);
return 0;
}
void func(struct Person p)
{
cout << " Person citizenship: " << p.citizenship<<endl;
cout << " Person age: " << p.age;
}
Output:
Here is a screenshot of the code:
Code Explanation:
The following are the limitations of structures:
We have organized the most frequently asked Linux Interview Questions and Answers that help...
Here are MuleSoft interview questions for fresher as well as experienced candidates to get the...
Web Development IDE's help programmers to easily code and debug websites/web apps. They help...
What is Hadoop? Apache Hadoop is an open source software framework used to develop data processing...
What is CI? Continuous integration is a software development method where members of the team can...
An eCommerce platform is a software application that helps online businesses to manage their...