Struct in C++

Yağmur Kocabıyık
5 min readMay 15, 2021

The purpose of most of the topics we learn in programming is to provide convenience to the code we write. Some make the code we write work more concisely, and some enable us to use different data types together. The data structures that I will describe in this blog are also a structure that enables us to use different data types together. We can keep different data types all in separate arrays, but this complicates our job. As I said at the beginning, our goal is to write code in an understandable and easy way.

The programmer can create own data types to deal with data consisting of different data types. Types consisting of different data types are called “Structured Data Types”. Since the programmer can define whatever data type own wants, it is impossible to put an upper limit on structured data types or to classify them. However, we can categorize them according to their foundation type.

ENUM

Enum is a structure that reduces code clutter, offering easier readability in places that require numerical comparison or processing within code.

For example, |enum days {MONDAY, TUESDAY, WEDNESDAY, THURSDAY}; (MONDAY = 0,

TUESDAY = 1,

WEDNESDAY = 2,

THURSDAY = 3).

So, enum numbers the constant values in it. The first constant starts with 0, since there is no special assignment to the values inside. The listed objects do not have to be capitalized, but since they are fixed numbers, it is more correct to write them in capital letters. Because the items do not change throughout the program, they can be named constant. They can only be changed in enum.

For example, |enum colors {WHITE = 6, BLACK, PURPLE = 5, PINK};

(WHITE = 6,

BLACK = 7,

PURPLE = 5,

PINK = 6).

As can be seen, a sequence number can be assigned to items in the enum. The other items take number increasing by 1 in the order of writing until the next numbering. The enumeration action can begin with any integer from any item. Multiple items can have the same numbering.

Structured data type is a data type that combines different data types and sees them as a unit. Structure should be defined by a name to call more easily. It is important that this name identifies the block it represents. Each of the variables in the body of the structure is a component of the its. When the empty structure is created, it does not take up any space in the main memory. When a variable is defined from that structure type, space is allocated in the main memory for the variable. The components of the structure are the components of the variable mentioned. “.” operator or “->” operator is used to access the components of the variable representing the structure. If we use a pointer in structure, members are accessed using “->” operator instead of “.” operator. I will explain about this topic.

The struct contains string, integer, and float numbers components. movie declares variable from a movies structure type. We use the ‘[]’ operator with the same logic we use in arrays. that is, the variable movie creates a 20 row array. For movie[0], we make a definition inside the first row zeroth index. Since the movie name is a string, it is defined with the strcpy function. Since the release year and IMDb of movie are numbers, it can be defined with ‘=’.

We know that a pointer can point to the address of a variable from any data type. The same is true for a variable of struct type. That is, we can access the components of the variables from the structure type using pointer.

p[0].a equals 30, and p[0].b equals 50. p[1].a equals 10, and p[1].b equals 40.

Using “p -> b” instead of “(* p) .a” to access components would be more understandable and correct. ‘p’ in the createPoint1 function is a pointer of structure type. ‘p’ in the createPoint2 function declares a variable of structure type. Let’s create a 10 row for p in main. Then let’s assign the value 10 to a and 20 to b for p [0]. And, assign the value 10 a and 40 to b for p[1]. Since the first function is called with a pointer, the values written into it are defined by address and whenever it is called it returns the value in the function. However, when the second function is called, the values typed are returned only in the function and are not remembered in the main.

UNION

In the main memory, the integer takes up 4 bytes of space, the double takes up 8 bytes of space, and the string takes up 20 bytes of space. Union takes up as much space in main memory as the string that takes up the most space. However, union takes up 24 bits, since double takes up 8 bits. Union would take up 20 bits of space, if it were float instead of double.

Union keeps different types of data together in a section of the memory. Thus, the main memory is used more efficient. At memory is freed up spaces as much as the variable that takes up the most space in the main memory. Also, if the union array is set up, an array that can contain different data types is defined. This, eliminates the restriction that the components of the array are of the same data type. So each of the specified component types fits in the allotted space. However, only one data type can be kept at in time. When the value is entered for the new data type, the previous data type is deleted.

That’s all I have to say about structure. If you want to see more examples, you can check my GitHub account. See you in my next blog.

References

Karaçay, T. (2017). C Programlamanın Temelleri. Cilt 3. (345–359)

--

--