Moment For Technology

C + + | c + + tutorial (eight) structure

Posted on Feb. 2, 2023, 12:50 a.m. by Iain Roberts
Category: c++ Tag: c++

8 structure

8.1 Basic concepts of structures

Structures are user-defined data types that allow users to store different data types

8.2 Structure definition and Use

Struct structureName{memberList};

Create syntax:

  1. struct StructureName variableName;
  2. Struct StructureName variableName{member1, member2...... };
  3. Create at definition timestruct StructureName{ memberList }variableName;

"Unavailable. Member" is used to access structure members

8.3 Structure array

Function: Puts custom structures into arrays for maintenance

StructureName variableName[dataNum]{{},{}... };

8.4 Pointer to a structure

Use: access members of a structure through Pointers

Use the operator - to access the properties of the structure through Pointers

8.5 Structure as parameter

Structures are also divided into values and addresses. Unlike an array, an array itself can be used as a pointer, but not a structure. A structure itself is a variable, so you have to address it to change the structure itself.

Note: This point is different from Python and Java and needs to be memorized separately.

8.6 Scenarios where const structures are applicable

Passing values is good (where is good?) However, every time a value is passed, the parameter to the argument must be copied, which is very resource-intensive. If we address it, we only need four bytes, which reduces the memory usage.

But there are pitfalls with pointer access to data, and you can change the value in a function.

Therefore, in order to protect the contents of the argument, we do not allow changes, so we use a constant pointer const structName* variableName as an argument.

About (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.