Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Arrays with Structs | Structs and Memory
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

bookArrays with Structs

Creating an array from structures is no different from creating an array with other data types.

Accessing the elements of such an array is similar to that of ordinary arrays by indexes.

As an example, let's create an array of structures that will store information about a person.

c

main

copy
1234567891011121314151617181920212223242526272829303132333435363738
#include <stdio.h> #include <string.h> // for the strcpy function // structure definition struct Person { char name[50]; int age; double height; }; int main() { // declaring an array of structures struct Person people[3]; // declaring an array of structures strcpy(people[0].name, "Alice"); people[0].age = 25; people[0].height = 1.75; strcpy(people[1].name, "Bob"); people[1].age = 30; people[1].height = 1.80; strcpy(people[2].name, "Charlie"); people[2].age = 35; people[2].height = 1.70; // output information about people from the array for (int i = 0; i < 3; ++i) { printf("Person %d:\n", i + 1); printf("Name: %s\n", people[i].name); printf("Age: %d\n", people[i].age); printf("Height: %.2f\n", people[i].height); printf("\n"); } return 0; }

Tarea

  1. Initialize a variable that will indicate the number of structures to be created;
  2. Create a pointer variable and place in it the address of an allocated memory location for a given number of structures in the array.
  3. Write code to autofill a dynamically created array with numbers from 0 to a number indicating the number of structures;
  4. Display the fields of all created structures in an array;
  5. Free allocated memory.

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 3. Capítulo 4
toggle bottom row

bookArrays with Structs

Creating an array from structures is no different from creating an array with other data types.

Accessing the elements of such an array is similar to that of ordinary arrays by indexes.

As an example, let's create an array of structures that will store information about a person.

c

main

copy
1234567891011121314151617181920212223242526272829303132333435363738
#include <stdio.h> #include <string.h> // for the strcpy function // structure definition struct Person { char name[50]; int age; double height; }; int main() { // declaring an array of structures struct Person people[3]; // declaring an array of structures strcpy(people[0].name, "Alice"); people[0].age = 25; people[0].height = 1.75; strcpy(people[1].name, "Bob"); people[1].age = 30; people[1].height = 1.80; strcpy(people[2].name, "Charlie"); people[2].age = 35; people[2].height = 1.70; // output information about people from the array for (int i = 0; i < 3; ++i) { printf("Person %d:\n", i + 1); printf("Name: %s\n", people[i].name); printf("Age: %d\n", people[i].age); printf("Height: %.2f\n", people[i].height); printf("\n"); } return 0; }

Tarea

  1. Initialize a variable that will indicate the number of structures to be created;
  2. Create a pointer variable and place in it the address of an allocated memory location for a given number of structures in the array.
  3. Write code to autofill a dynamically created array with numbers from 0 to a number indicating the number of structures;
  4. Display the fields of all created structures in an array;
  5. Free allocated memory.

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 3. Capítulo 4
toggle bottom row

bookArrays with Structs

Creating an array from structures is no different from creating an array with other data types.

Accessing the elements of such an array is similar to that of ordinary arrays by indexes.

As an example, let's create an array of structures that will store information about a person.

c

main

copy
1234567891011121314151617181920212223242526272829303132333435363738
#include <stdio.h> #include <string.h> // for the strcpy function // structure definition struct Person { char name[50]; int age; double height; }; int main() { // declaring an array of structures struct Person people[3]; // declaring an array of structures strcpy(people[0].name, "Alice"); people[0].age = 25; people[0].height = 1.75; strcpy(people[1].name, "Bob"); people[1].age = 30; people[1].height = 1.80; strcpy(people[2].name, "Charlie"); people[2].age = 35; people[2].height = 1.70; // output information about people from the array for (int i = 0; i < 3; ++i) { printf("Person %d:\n", i + 1); printf("Name: %s\n", people[i].name); printf("Age: %d\n", people[i].age); printf("Height: %.2f\n", people[i].height); printf("\n"); } return 0; }

Tarea

  1. Initialize a variable that will indicate the number of structures to be created;
  2. Create a pointer variable and place in it the address of an allocated memory location for a given number of structures in the array.
  3. Write code to autofill a dynamically created array with numbers from 0 to a number indicating the number of structures;
  4. Display the fields of all created structures in an array;
  5. Free allocated memory.

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!

Creating an array from structures is no different from creating an array with other data types.

Accessing the elements of such an array is similar to that of ordinary arrays by indexes.

As an example, let's create an array of structures that will store information about a person.

c

main

copy
1234567891011121314151617181920212223242526272829303132333435363738
#include <stdio.h> #include <string.h> // for the strcpy function // structure definition struct Person { char name[50]; int age; double height; }; int main() { // declaring an array of structures struct Person people[3]; // declaring an array of structures strcpy(people[0].name, "Alice"); people[0].age = 25; people[0].height = 1.75; strcpy(people[1].name, "Bob"); people[1].age = 30; people[1].height = 1.80; strcpy(people[2].name, "Charlie"); people[2].age = 35; people[2].height = 1.70; // output information about people from the array for (int i = 0; i < 3; ++i) { printf("Person %d:\n", i + 1); printf("Name: %s\n", people[i].name); printf("Age: %d\n", people[i].age); printf("Height: %.2f\n", people[i].height); printf("\n"); } return 0; }

Tarea

  1. Initialize a variable that will indicate the number of structures to be created;
  2. Create a pointer variable and place in it the address of an allocated memory location for a given number of structures in the array.
  3. Write code to autofill a dynamically created array with numbers from 0 to a number indicating the number of structures;
  4. Display the fields of all created structures in an array;
  5. Free allocated memory.

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