Last Updated:

C++ : The enumerated type | Structure | Merge | Examples

The enumerated type

1. Enumerable type

An enumerated type specifies a type that is a subset of an integer type.

Declaring an enumerable variable specifies the name of the variable and defines a list of named constants called an enumeration list:

enum [<tag>] {< enumeration list>} <ascribe> [, <ascribe> ...]; enum <tag> <ascribe> [, <ascribe> ...];

The tag is designed to distinguish between multiple enumerated types declared in the same program.

The enumeration list contains one or more constructs of the form:

<IDent> [= <constant expression>]

Constructions in the list are separated by commas. Each identifier names an enumeration list item. By default, if you do not specify a constant expression, the first element is set to 0, the next element is set to 1, and so on.

Record = <constant expression> changes the sequence of values that is being hushed up. The element whose ID precedes the record = <constant expression> accepts the value specified by this constant expression. A constant expression must be of type int and can be either positive or negative. The next item in the list receives a value equal to <constant expression> + 1, unless its value is explicitly set by another constant expression.

An enumeration list can contain items that have the same values associated with the same values, but each identifier in the list must be unique. In addition, the id of the enumeration list item must be different from the item IDs of all other enumeration lists, as well as from other identifiers.

enum Weekdays {SA, SU, MO, TU, WE, TH, FR}; 
enum Weekdays {SA, SU = 0, MO, TU, WE, TH, FR};SA and SU have the same value
void main() 
{ enum Weekdays d1 = SA, d2 = SU, d3 = WE, d4; 
d4 = 2;Error!
d4 = d1 + d2;Error!
d4 = (enum Weekdays)(d1 + d2);It is possible, but the result
d4 = (enum Weekdays)(d1 - d2);may not get
d4 = (enum Weekdays)(TH * FR);to the definition area
d4 = (enum Weekdays)(WE / TU);Enumeration

2. Structures

The structure allows you to combine in one object a set of values that can have different types. However, the C language implements a very limited set of operations on structures as a whole: passing a function as an argument, returning as a function value, getting an address. You can assign one structure to another if they are of the same type.

A structure declaration specifies the name of the structure type and/or a sequence of declarations of variables called structure elements. These elements can have different types.
struct [<tag>] {<list of item declarations>} <ascriber> [, <ascriptor> ...]; struct <tag> <descriptor> [, <scriptor> ...];

The tag is designed to distinguish between multiple structures declared in a single program.

An item declaration list is a sequence of one or more variable declarations. Each variable declared in this list is called a structure element. The peculiarity of the syntax of declarations of structure elements is that they cannot contain memory class specifications and initializers. Elements of a structure can be of a base type, or they can be an array, pointer, union, or structure.

{ char str[50]; 
int a, b;Declare the structure without specifying a tag
} s;and immediately declare a variable
struct S 
{ char str[50]; 
int a, b; 
};Declare a structure with an S tag
struct S s;Declare a variable

An element of a structure cannot be a structure of the same type as it is contained. However, it can be a pointer to the type of structure in which it belongs. The size of the pointer is standard, so the compiler knows how much memory will be required for the pointer. To work with a pointer, you need to know the size of the type it points to, but by the time you work with the pointer, the structure will be fully declared, and, therefore, its size will be known.

The identifiers of the elements of the structure should differ from each other. The identifiers of the elements of different structures can coincide.

To initialize the structure, as well as other composite types, you need to write the list of initializers separated by a comma in curly braces.

struct S s = {"Str", 0, 1};Use the S tag declared in the previous example

The choice of a structure element is carried out using one of the following constructs:
<change> . <Dident of the structure element> <pointer> -> <indelifier of the structure element>

An item picker expression allows you to access a structure element. The expression has the value and type of the selected item.

struct S s, *p = &s;Declare a variable and a pointer to which we enter the address of the variable sps
s.a = 10; 
p->b = 20; 

The two structures are of different types, even if their declarations are exactly the same.

2.1. Example

Enter an array of structures and search for any set of parameters.


#include <sspanio.h>
#include <string.h>
#include <conio.h>

struct S // Declare a structure consisting
 {char str[21]; // from string and integer
   int a;

structS s[10]; // Declare an array of structures

void main(int argc, char *argv[])
{ int n, a, i, check = 0; // Variable a contains a number that will be compared with a field of structure a.
                                       // The check variable specifies whether to use this parameter for the search.
  char str[21] = ""; // The str variable contains a string that will be compared to a field of the str structure.
                                       // If str contains an empty string, this parameter is not used for searching.
  FILE *in, *out;

  if (argc < 3)
   { printf("Too few arguments.\n"); return; }
  if ((in = fopen(argv[1], "r")) == NULL)
   { printf("It is impossible to open file '%s'.\n", argv[1]);
  if ((out = fopen(argv[2], "w")) == NULL)
   { printf("It is impossible to open file '%s'.\n", argv[2]);
     fclose(in); return;
  for (n = 0; !feof(in); n++)
   fscanf(in, "%s%d", s[n].str, &s[n].a);
  printf("Use Str for search? "); ans = getche();
  if (ans == 'y' || ans == 'Y')
   { printf("\nInput string for search: "); scanf("%s",str); }
  printf("Use A for search? "); ans = getche();
  if (ans == 'y' || ans == 'Y')
   { check = 1; printf("\nInput A: "); scanf("%d", &a); }
  for (i = 0; i < n; i++)
   if ((!*str || strcmp(str, s[i].str) == 0) && // This condition checks the contents of the structure for equality
       (!check || a == s[i].a)) // search parameters, taking into account the need to compare with this parameter
    fprintf(out, "%-30s %3d\n", s[i].str, s[i].a);

3. Associations


Merging allows you to store values of different types in the same object at different times. In the process of declaring a union, it is associated with a set of value types that can be stored in a given association. A union can store only one type of value from a set at a time. It is up to the programmer to control what type of value is currently stored in the association.
union [< tag>] {<list of item declarations>} <ascriber> [, <ascriber> ...]; union <tag> <ascriber> [, <ascriptor> ...];

The tag is designed to distinguish between multiple associations declared in the same program.

The memory that is allocated to a joining variable is determined by the size of the longest union element. All merge elements are placed in the same memory area from the same address. The value of the current union element is lost when another union element is assigned a value.

#include <stdio.h> void main() {  union { float f; long int i; } u; printf("Input float number: "); scanf("%f", &u.f); printf("Internal: %08x\n\n", u.i); }