Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Accessing Struct Fields | Introduction to Structs
C Structs

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.

<struct>.<field>;

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

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

main.c

main.c

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
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.

main.c

main.c

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
Note

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

Opgave

Swipe to start coding

You have a Product structure that stores information about a product in a store: its name, price, and quantity. Your task is to calculate the total cost of a product by multiplying its price by its quantity.

The function productCost takes a Product as a parameter.

  1. Inside productCost, multiply the price field by the quantity field.
  2. In main, create a Product object with a name, price, and quantity.
  3. Use printf to display the product’s name, price, quantity, and total cost (by calling productCost).

Løsning

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 5
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

Can you show me an example of how to use the `.` operator with a struct?

What does the `srtcopy()` function do?

How do I display all the fields of the silicon struct?

close

Awesome!

Completion rate improved to 4.17

bookAccessing Struct Fields

Stryg for at vise menuen

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.

<struct>.<field>;

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

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

main.c

main.c

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
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.

main.c

main.c

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
Note

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

Opgave

Swipe to start coding

You have a Product structure that stores information about a product in a store: its name, price, and quantity. Your task is to calculate the total cost of a product by multiplying its price by its quantity.

The function productCost takes a Product as a parameter.

  1. Inside productCost, multiply the price field by the quantity field.
  2. In main, create a Product object with a name, price, and quantity.
  3. Use printf to display the product’s name, price, quantity, and total cost (by calling productCost).

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 5
single

single

some-alt