Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Accessing Struct Fields | Introduction to Structs
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

bookAccessing Struct Fields

After we have created a structure to describe a chemical element, we can finally interact with it.

In order to access the information contained in the structure, we must use the . operator.

The . operator allows us to access the elements (fields) of the structure directly.

This way we get access to a member of the structure.

Let's try to display information about silicon on the screen:

c

main

copy
123456789101112131415161718192021
#include "stdio.h" struct chemicalElement { char name[3]; int atomicNumber; int valency; double mass; }; int main() { struct chemicalElement silicon = { "Si", 14, 4, 28.08 }; printf("Name: %s\n", silicon.name); printf("Atomic Number: %d\n", silicon.atomicNumber); printf("Valency: %d\n", silicon.valency); printf("Mass: %f", silicon.mass); return 0; }

Note

Pointers are also widely used to access structure fields, but we'll cover that later.

If you used the second method of creating a struct-variable, you need to use the srtcopy() function to output silicon.name.

c

main

copy
123456789101112131415161718192021222324
#include "stdio.h" struct chemicalElement { char name[3]; int atomicNumber; int valency; double mass; } silicon; int main() { strcpy(silicon.name,"Si"); // using `strcpy()` silicon.atomicNumber = 14; silicon.valency = 4; silicon.mass = 28.08; printf("Name: %s\n", silicon.name); printf("Atomic Number: %d\n", silicon.atomicNumber); printf("Valency: %d\n", silicon.valency); printf("Mass: %f", silicon.mass); return 0; }

Note

The strcpy() function is used to copy the specified (“Si”) string to the name[3] field of the created variable.

Tarea

  1. Display the valence of natrium;
  2. Display the atomic mass of aluminum;
  3. Display the atomic number of zinc.

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 1. Capítulo 5
toggle bottom row

bookAccessing Struct Fields

After we have created a structure to describe a chemical element, we can finally interact with it.

In order to access the information contained in the structure, we must use the . operator.

The . operator allows us to access the elements (fields) of the structure directly.

This way we get access to a member of the structure.

Let's try to display information about silicon on the screen:

c

main

copy
123456789101112131415161718192021
#include "stdio.h" struct chemicalElement { char name[3]; int atomicNumber; int valency; double mass; }; int main() { struct chemicalElement silicon = { "Si", 14, 4, 28.08 }; printf("Name: %s\n", silicon.name); printf("Atomic Number: %d\n", silicon.atomicNumber); printf("Valency: %d\n", silicon.valency); printf("Mass: %f", silicon.mass); return 0; }

Note

Pointers are also widely used to access structure fields, but we'll cover that later.

If you used the second method of creating a struct-variable, you need to use the srtcopy() function to output silicon.name.

c

main

copy
123456789101112131415161718192021222324
#include "stdio.h" struct chemicalElement { char name[3]; int atomicNumber; int valency; double mass; } silicon; int main() { strcpy(silicon.name,"Si"); // using `strcpy()` silicon.atomicNumber = 14; silicon.valency = 4; silicon.mass = 28.08; printf("Name: %s\n", silicon.name); printf("Atomic Number: %d\n", silicon.atomicNumber); printf("Valency: %d\n", silicon.valency); printf("Mass: %f", silicon.mass); return 0; }

Note

The strcpy() function is used to copy the specified (“Si”) string to the name[3] field of the created variable.

Tarea

  1. Display the valence of natrium;
  2. Display the atomic mass of aluminum;
  3. Display the atomic number of zinc.

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 1. Capítulo 5
toggle bottom row

bookAccessing Struct Fields

After we have created a structure to describe a chemical element, we can finally interact with it.

In order to access the information contained in the structure, we must use the . operator.

The . operator allows us to access the elements (fields) of the structure directly.

This way we get access to a member of the structure.

Let's try to display information about silicon on the screen:

c

main

copy
123456789101112131415161718192021
#include "stdio.h" struct chemicalElement { char name[3]; int atomicNumber; int valency; double mass; }; int main() { struct chemicalElement silicon = { "Si", 14, 4, 28.08 }; printf("Name: %s\n", silicon.name); printf("Atomic Number: %d\n", silicon.atomicNumber); printf("Valency: %d\n", silicon.valency); printf("Mass: %f", silicon.mass); return 0; }

Note

Pointers are also widely used to access structure fields, but we'll cover that later.

If you used the second method of creating a struct-variable, you need to use the srtcopy() function to output silicon.name.

c

main

copy
123456789101112131415161718192021222324
#include "stdio.h" struct chemicalElement { char name[3]; int atomicNumber; int valency; double mass; } silicon; int main() { strcpy(silicon.name,"Si"); // using `strcpy()` silicon.atomicNumber = 14; silicon.valency = 4; silicon.mass = 28.08; printf("Name: %s\n", silicon.name); printf("Atomic Number: %d\n", silicon.atomicNumber); printf("Valency: %d\n", silicon.valency); printf("Mass: %f", silicon.mass); return 0; }

Note

The strcpy() function is used to copy the specified (“Si”) string to the name[3] field of the created variable.

Tarea

  1. Display the valence of natrium;
  2. Display the atomic mass of aluminum;
  3. Display the atomic number of zinc.

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!

After we have created a structure to describe a chemical element, we can finally interact with it.

In order to access the information contained in the structure, we must use the . operator.

The . operator allows us to access the elements (fields) of the structure directly.

This way we get access to a member of the structure.

Let's try to display information about silicon on the screen:

c

main

copy
123456789101112131415161718192021
#include "stdio.h" struct chemicalElement { char name[3]; int atomicNumber; int valency; double mass; }; int main() { struct chemicalElement silicon = { "Si", 14, 4, 28.08 }; printf("Name: %s\n", silicon.name); printf("Atomic Number: %d\n", silicon.atomicNumber); printf("Valency: %d\n", silicon.valency); printf("Mass: %f", silicon.mass); return 0; }

Note

Pointers are also widely used to access structure fields, but we'll cover that later.

If you used the second method of creating a struct-variable, you need to use the srtcopy() function to output silicon.name.

c

main

copy
123456789101112131415161718192021222324
#include "stdio.h" struct chemicalElement { char name[3]; int atomicNumber; int valency; double mass; } silicon; int main() { strcpy(silicon.name,"Si"); // using `strcpy()` silicon.atomicNumber = 14; silicon.valency = 4; silicon.mass = 28.08; printf("Name: %s\n", silicon.name); printf("Atomic Number: %d\n", silicon.atomicNumber); printf("Valency: %d\n", silicon.valency); printf("Mass: %f", silicon.mass); return 0; }

Note

The strcpy() function is used to copy the specified (“Si”) string to the name[3] field of the created variable.

Tarea

  1. Display the valence of natrium;
  2. Display the atomic mass of aluminum;
  3. Display the atomic number of zinc.

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