C++C++11C++14C++17Introduction to C++Learn C++

Learn about Data Structures in C & C++

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;

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,

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;

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,

Creating a Data Object Arrays (Structure Array)

We can create create structure arrays as below,

Setting Member variables of a Structure

Each struct object member can be set or modified as below,

Printing Structure Members

Each member of a struct can be printed as below

Data Object Pointers (Pointer of Struct)

Structure objects can be defined as pointers as below too,

They should be allocated in the memory to hold variables.

Full Example

Get started building powerful apps with C++Builder!

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20

What Is The Stack (std::stack) In Modern C++?

C++C++11C++14C++17C++20Learn C++

What Is The Queue (std::queue) In Modern C++?

C++C++11C++14C++17Learn C++SyntaxTemplates

What Are The Logical Operation Metafunctions In Modern C++?

C++C++14C++17C++20Learn C++

What Are The Deprecated C++14 Features In C++17?

Worth reading...
Learn to Use Object Arrays and Object Pointer Arrays in C++