Language: EN

que-es-una-struct

What is and how to use Structs

Los STRUCT are data structures that allow grouping variables of different types into a single entity.

If we had said that variables are like boxes, the STRUCTS are like a lunchbox with a combination plate. It is a container that contains other boxes, so that the peas do not mix with the rice.

  • It has other departments inside
  • Each department has the necessary size
  • Together they form a single container

curso-programacion-que-es-struct

This lunchbox is your Struct

For example, imagine that you have to store person data. For the example, a person will have a name, date of birth, and email. It is logical that you have a grouping of variables that allows you to work with them together.

curso-programacion-struct

That is precisely a STRUCT, a grouping of variables that we create to work with them at the same time. In this way it is much more comfortable than having the loose variables.

Unlike objects, STRUCT are generally used to group simple data. Also, they lack associated functionalities or methods, they only contain data.

The STRUCT are useful when you need to group related variables, but you do not require the complexity and flexibility of objects.

Examples of Structs in different languages

Almost all languages have some type of variable grouping, which may be called Struct or something else. But, basically it is the same.

Logically the syntax will depend on the programming language we are using. Let’s see examples in some popular languages,

struct Persona
{
    public string Nombre;
    public int Edad;
    public double Altura;
}

// Access to structure elements
Persona persona;
persona.Nombre = "Juan";
persona.Edad = 25;
var altura = persona.Altura;
struct Persona
{
    std::string Nombre;
    int Edad;
    double Altura;
};

// Access to structure elements
Persona persona;
persona.Nombre = "Juan";
persona.Edad = 25;
double altura = persona.Altura;
const persona = {
    nombre: "Juan",
    edad: 25,
    altura: 1.75
};

// Access to structure elements
persona.nombre = "Juan";
persona.edad = 25;
let altura = persona.altura;
class Persona:
    def __init__(self, nombre, edad, altura):
        self.nombre = nombre
        self.edad = edad
        self.altura = altura

# Access to structure elements
persona = Persona("Juan", 25, 1.75)

In the previous examples, we create structures that represent information about a person, including their name, age, and height.

The syntax varies slightly between languages, but the concept is the same: define an entity that groups related data.

Internal operation Advanced

Internally in most programming languages, a STRUCT is stored in memory in a contiguous manner. When an instance of a STRUCT is created, the system assigns a block of memory that can contain all its members.

For example, if you have a STRUCT called Person with members for the name, date of birth, and email, memory will be reserved to store this data consecutively, in the order in which they were declared.

When you access a member of a STRUCT, the system can directly calculate its position in memory by adding an offset from the base address of the STRUCT.

Because the members of a STRUCT are stored contiguously, access to this data is very efficient. It is simply a memory access operation with an offset.

On the other hand, in many languages, STRUCT are value types (not references). This means that a copy of the STRUCT is created and that copy is passed to the function. Depending on the size of the STRUCT, this can have implications for performance and the amount of memory used.

So it is convenient for you to know the implementation details in your particular language. But don’t get carried away too much, in most languages the compiler makes decisions and can treat references as values, or vice versa, if it understands that it is more efficient.