Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Nested Structs | Advanced Structs Usage
C Structs
course content

Contenido del Curso

C Structs

C Structs

1. Introduction to Structs
2. Pointers and Structs
3. Structs and Memory
4. Advanced Structs Usage
5. Implementing Data Structures

bookNested Structs

In addition to the basic primitive data types such as int or char, arrays and pointers, you can use other structures as members within your structure.

Several methods can be used:

  • Declare and define the NestedStruct structure inside the OuterStruct structure (embedded nested struct);
  • Declare the NestedStruct structure separately from the OuterStruct structure, create an instance of the NestedStruct structure inside the OuterStruct structure (separate nested struct).

The difference is that when a nested structure is declared separately, that structure can be used in many other structures.

If an instance of a nested structure was created outside of an external structure, then the structure will not be nested.

You can access the fields of a nested structure directly through the "." operator or through a pointer and the -> operator.

An example of using nested structures:

c

main

copy
123456789101112131415161718192021222324252627
#include <stdio.h> // structure for address struct Address { char street[50]; char city[50]; char index[10]; }; // structure for person struct Person { char name[50]; int age; struct Address address; // nested structure }; int main() { // initialization of Person structure struct Person person = { "Sherlock Holmes", 27, {"Baker Street", "London", "221B"} }; // display information about the person and his address printf("Name: %s\n", person.name); printf("Age: %d\n", person.age); printf("Address: %s, %s, %s\n", person.address.index, person.address.street, person.address.city); return 0; }

Tarea

  1. Define a nested structure education;
  2. Create an instance of the Student structure;
  3. Display the student's name and age;
  4. Display information about the student’s education.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. Capítulo 1
toggle bottom row

bookNested Structs

In addition to the basic primitive data types such as int or char, arrays and pointers, you can use other structures as members within your structure.

Several methods can be used:

  • Declare and define the NestedStruct structure inside the OuterStruct structure (embedded nested struct);
  • Declare the NestedStruct structure separately from the OuterStruct structure, create an instance of the NestedStruct structure inside the OuterStruct structure (separate nested struct).

The difference is that when a nested structure is declared separately, that structure can be used in many other structures.

If an instance of a nested structure was created outside of an external structure, then the structure will not be nested.

You can access the fields of a nested structure directly through the "." operator or through a pointer and the -> operator.

An example of using nested structures:

c

main

copy
123456789101112131415161718192021222324252627
#include <stdio.h> // structure for address struct Address { char street[50]; char city[50]; char index[10]; }; // structure for person struct Person { char name[50]; int age; struct Address address; // nested structure }; int main() { // initialization of Person structure struct Person person = { "Sherlock Holmes", 27, {"Baker Street", "London", "221B"} }; // display information about the person and his address printf("Name: %s\n", person.name); printf("Age: %d\n", person.age); printf("Address: %s, %s, %s\n", person.address.index, person.address.street, person.address.city); return 0; }

Tarea

  1. Define a nested structure education;
  2. Create an instance of the Student structure;
  3. Display the student's name and age;
  4. Display information about the student’s education.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. Capítulo 1
toggle bottom row

bookNested Structs

In addition to the basic primitive data types such as int or char, arrays and pointers, you can use other structures as members within your structure.

Several methods can be used:

  • Declare and define the NestedStruct structure inside the OuterStruct structure (embedded nested struct);
  • Declare the NestedStruct structure separately from the OuterStruct structure, create an instance of the NestedStruct structure inside the OuterStruct structure (separate nested struct).

The difference is that when a nested structure is declared separately, that structure can be used in many other structures.

If an instance of a nested structure was created outside of an external structure, then the structure will not be nested.

You can access the fields of a nested structure directly through the "." operator or through a pointer and the -> operator.

An example of using nested structures:

c

main

copy
123456789101112131415161718192021222324252627
#include <stdio.h> // structure for address struct Address { char street[50]; char city[50]; char index[10]; }; // structure for person struct Person { char name[50]; int age; struct Address address; // nested structure }; int main() { // initialization of Person structure struct Person person = { "Sherlock Holmes", 27, {"Baker Street", "London", "221B"} }; // display information about the person and his address printf("Name: %s\n", person.name); printf("Age: %d\n", person.age); printf("Address: %s, %s, %s\n", person.address.index, person.address.street, person.address.city); return 0; }

Tarea

  1. Define a nested structure education;
  2. Create an instance of the Student structure;
  3. Display the student's name and age;
  4. Display information about the student’s education.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

In addition to the basic primitive data types such as int or char, arrays and pointers, you can use other structures as members within your structure.

Several methods can be used:

  • Declare and define the NestedStruct structure inside the OuterStruct structure (embedded nested struct);
  • Declare the NestedStruct structure separately from the OuterStruct structure, create an instance of the NestedStruct structure inside the OuterStruct structure (separate nested struct).

The difference is that when a nested structure is declared separately, that structure can be used in many other structures.

If an instance of a nested structure was created outside of an external structure, then the structure will not be nested.

You can access the fields of a nested structure directly through the "." operator or through a pointer and the -> operator.

An example of using nested structures:

c

main

copy
123456789101112131415161718192021222324252627
#include <stdio.h> // structure for address struct Address { char street[50]; char city[50]; char index[10]; }; // structure for person struct Person { char name[50]; int age; struct Address address; // nested structure }; int main() { // initialization of Person structure struct Person person = { "Sherlock Holmes", 27, {"Baker Street", "London", "221B"} }; // display information about the person and his address printf("Name: %s\n", person.name); printf("Age: %d\n", person.age); printf("Address: %s, %s, %s\n", person.address.index, person.address.street, person.address.city); return 0; }

Tarea

  1. Define a nested structure education;
  2. Create an instance of the Student structure;
  3. Display the student's name and age;
  4. Display information about the student’s education.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 4. Capítulo 1
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
some-alt