What is Structure in C – All You Need to Know
Have you ever wondered what you would do if you had to store two or more data types together in C Programming? The answer to this question is Structure in C, derived from ALGOL 68 struct data type. It is used to bind two or more data types together in a single type. Certain types, like integers and decimal values, are used singly to store values in C. The structure function is implemented to assemble the data types for a clean arrangement.
What is a Structure?
The structure is a user-defined data type that stores or binds two or more data types together. It does not take up any space or memory unless some variables are defined. It is usually defined by the struct keyword, followed by a unique identifier for the structure known as a structure tag. It allows you to store data in an efficient and organized way.
Syntax of Structure in C
The keyword struct is used while you are writing the program for Structure. It is always put at the beginning of the syntax. The rest of the syntax can be customized according to the program’s requirements.
Syntax:
struct structure_name
{
Int x;
Char y;
}
Why Do We Use Structure?
Would you like it if you had to scroll through millions of pages to get to a specific amount of essential data you have been looking for a long time? To avoid that, we use Structure. It helps you bind the same or different data types, making work more accessible and organized.
For example, you have to store data related to company employees, like their ids, contact numbers, and addresses. In this case, creating an array, integer, and character variable for almost 1000 candidates would not be feasible. Hence you can use Structure in C to organize your data and work more simply.
How to Create a Structure?
The struct keyword is used to create a structure in C, followed by the structure’s tag name. Then the body where the required data members can be primitive or user-defined is added.
For example, if you have to create a structure for employee data, the syntax will involve data members to be of any data type. It will also include data member definition of any data type like character array or integer.
#include <iostream>
#include <string>
using namespace std;
int main() {
struct {
int myNum;
string myString;
} myStructure;
myStructure.myNum = 1;
myStructure.myString = "Hello World!";
cout << myStructure.myNum << "\n";
cout << myStructure.myString << "\n";
return 0;
}
Declaring Structure Variables
Structure variables are created to use the properties of structure in C. There are two ways to declare variables.
1. Structure Declaration
#include <iostream>
using namespace std;
struct Point
{
int x, y;
} z1; // The variable z1 is declared with 'Point'
2. Separate Declaration Like Basic Types
#include <iostream>
using namespace std;
struct Point
{
int x, y;
};
int main()
{
struct Point z1; // The variable z1 is declared like a normal variable
}
Initializing Structure Members
The initialization of structure members means assigning values to them. Structure members cannot be initialized during the declaration. You will have to allocate memory to the structure variable in order to initialize the structure members.
The enlisted three ways are followed to initialize structure members:
- Dot ‘.’ Operator
You can access any structure member using the dot ‘.’ operator to initialize the values according to the data type of that structure member.
For example,
#include <iostream>
using namespace std;
struct Point {
int x = 1;
int y = 2;
};
int main()
{
struct Point p1;
cout << "x = " << p1.x << ", y = " << p1.y<<endl;
p1.y = 30;
cout << "x = " << p1.x << ", y = " << p1.y;
return 0;
}
You have created the structure variable in the example above and then accessed the member for initializing by using the dot operator.
- Curly Braces ‘{}’
You can initialize the structure members during the structure variable declaration by using the curly ‘{}’ braces. It is efficient to use when you have all the data members to initialize.
For example,
#include <iostream>
using namespace std;
struct Point {
int x, y;
};
int main()
{
struct Point p1 = { 0, 1 };
}
- Designated Initializers
The designated initializers allow you to initialize structure members in any order. It is not important to maintain a specific order while initializing through designated initializers. They are used when you want to initialize only a few of the structure members. You can learn more about structure members in C through this in-depth C and C++ Course.
For example,
#include <iostream>
struct Point2D{
int a;
int b;
};
class Point3D{
public:
int a;
int b;
int c;
};
int main(){
std::cout << std::endl;
Point2D point2D{1, 2}; // (1)
Point3D point3D{1, 2, 3}; // (2)
std::cout << "point2D: " << point2D.a << " " << point2D.b << std::endl;
std::cout << "point3D: " << point3D.a << " " << point3D.b << " " << point3D.c << std::endl;
std::cout << std::endl;
}
Advantages and Limitations of Structure
The following table showcases the advantages and limitations of structure:
Advantages | Limitations |
It reduces complex operations. | It does not support data-hiding properties. |
It increases productivity as it allows you to organize data faster. | The functions cannot be defined inside the Structure. |
It enhances the code readability. | There may be some memory loss if the Structure members are not aligned. |
It works well with maintaining mathematical operations. | You cannot initialize data members inside structures. |
Conclusion
Structure in C is a user-defined data type and is created using the struct keyword. It works in binding the two or more data types together. It also helps organize data effectively and takes less time to build a better presentation of the stored data.