In general, when we do programming we have a lot of variables and some of these variables are properties of an object, for example if we want to make a database about students in C++, each student has similar properties to store. We can generalize all these properties in a single group. In this post we will explain structs in detail.
In C & C++ programming; a Data Structure (struct) is a data block in memory and group of data elements grouped together with a name, these data elements are variables (integers, floats, strings …) called as members, they can be defined to a new variable under a single name. They can have different types and different lengths. Data structures can be declared by using struct statement, using the following syntax;
1 2 3 4 |
struct struct_name { // List data members here } dataobject_names; |
Structs are primarily used in C programming language, you can store your data as same in C++ too. The main difference between a struct and a class is classes can obtain both data and functions while structs has data only, functions can be used externally. One of the benefit of structs are they have fixed size, it is easy to read and write multiple data blocks in exact position in a file.
Creating a new Structure with its Members
Let’s give a simple example to define a student structure,
1 2 3 4 5 6 |
struct st_student { char name[32]; int age; float weight; } ; |
Creating a Data Object from a Structure
Now we can define a data structure object as below;
Using this st_student struct definition we can create data object arrays as below;
1 |
struct st_student student1; |
Here student1 data object has same name, age, weight members as in st_student.
Creating a Structure and a Data Object Together
In addition to this example, we can define these both new structure and a structure object together as below,
1 2 3 4 5 6 |
struct st_student { char name[32]; int age; float weight; } student1; |
Creating a Data Object Arrays (Structure Array)
We can create create structure arrays as below,
1 |
struct st_student students[100]; |
Setting Member variables of a Structure
Each struct object member can be set or modified as below,
1 2 3 4 5 6 7 |
strcpy(students[0].name, "John Smith"); students[0].age=25; students[0].weight=68.5; strcpy(students[1].name, "Kate White"); students[1].age=27; students[1].weight=55.2; |
Printing Structure Members
Each member of a struct can be printed as below
1 2 3 4 5 6 7 |
std:: cout << "Name:" << students[0].name << '\n'; std:: cout << "Age:" << students[0].age << '\n'; std:: cout << "Weight:" << students[0].weight << '\n'; std:: cout << "Name:" << students[1].name << '\n'; std:: cout << "Age:" << students[1].age << '\n'; std:: cout << "Weight:" << students[1].weight << '\n'; |
Data Object Pointers (Pointer of Struct)
Structure objects can be defined as pointers as below too,
1 |
struct st_student *student1; |
They should be allocated in the memory to hold variables.
Full Example
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 27 |
struct st_student { char name[32]; int age; float weight; }; struct st_student students[100]; int main() { strcpy(students[0].name, "John Smith"); students[0].age=25; students[0].weight=68.5; strcpy(students[1].name, "Kate White"); students[1].age=27; students[1].weight=55.2; std:: cout << "Name:" << students[0].name << '\n'; std:: cout << "Age:" << students[0].age << '\n'; std:: cout << "Weight:" << students[0].weight << '\n'; std:: cout << "Name:" << students[1].name << '\n'; std:: cout << "Age:" << students[1].age << '\n'; std:: cout << "Weight:" << students[1].weight << '\n'; } |
Get started building powerful apps with C++Builder!
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition