Menu
Coddy logo textTech

Accessing Struct Members

Part of the Logic & Flow section of Coddy's C journey — lesson 41 of 63.

Now that you can create struct variables, you need to learn how to work with the data stored inside them. The dot operator (.) is your key to accessing and modifying the individual members of a struct variable.

The syntax for accessing struct members is straightforward:

structVariable.memberName

Using our Point struct example, you can both read from and write to its members:

struct Point p1;

// Writing to struct members
p1.x = 10;
p1.y = 20;

// Reading from struct members
printf("Point coordinates: (%d, %d)\n", p1.x, p1.y);

The dot operator works exactly like accessing any other variable - you can use it in assignments, arithmetic operations, function calls, or anywhere you would use a regular variable of that type. Each member behaves as an independent variable of its declared type, allowing you to manipulate the struct's data in an organized and intuitive way.

challenge icon

Challenge

Easy

Create a C program that demonstrates accessing and modifying struct members using the dot operator. Your program should:

  1. Define a struct named Car with the following members:
    • An integer year to store the manufacturing year
    • A character array brand with size 20 to store the car brand
    • An integer mileage to store the current mileage
    • A float price to store the car's price
  2. Create a Car variable named myCar
  3. Read the following input values and assign them to the struct members using the dot operator:
    • Read an integer for the year and assign it to myCar.year
    • Read a string for the brand and assign it to myCar.brand
    • Read an integer for the mileage and assign it to myCar.mileage
    • Read a float for the price and assign it to myCar.price
  4. After reading all values, perform the following operations:
    • Increase the mileage by 1000 using myCar.mileage += 1000;
    • Decrease the price by 500.0 using myCar.price -= 500.0;
  5. Print the car information using the dot operator to access each member in this exact format:
    • Car Details:
    • Year: [year]
    • Brand: [brand]
    • Mileage: [mileage]
    • Price: [price]

This challenge tests your understanding of the dot operator for both reading from and writing to struct members. You'll practice assigning values to struct members, performing arithmetic operations on them, and accessing them for output.

For example, if the input is:

2020
Toyota
50000
25000.50

Your output should be:

Car Details:
Year: 2020
Brand: Toyota
Mileage: 51000
Price: 24500.50

Cheat sheet

The dot operator (.) is used to access and modify individual members of a struct variable.

Syntax for accessing struct members:

structVariable.memberName

Example of reading from and writing to struct members:

struct Point p1;

// Writing to struct members
p1.x = 10;
p1.y = 20;

// Reading from struct members
printf("Point coordinates: (%d, %d)\n", p1.x, p1.y);

The dot operator can be used in assignments, arithmetic operations, function calls, or anywhere you would use a regular variable of that type. Each member behaves as an independent variable of its declared type.

Try it yourself

#include <stdio.h>
#include <string.h>

// TODO: Define the Car struct here

int main() {
    // TODO: Create a Car variable named myCar
    
    // Read input values
    int year;
    char brand[20];
    int mileage;
    float price;
    
    scanf("%d", &year);
    scanf("%s", brand);
    scanf("%d", &mileage);
    scanf("%f", &price);
    
    // TODO: Assign the input values to struct members using dot operator
    
    // TODO: Modify the mileage and price as specified
    
    // TODO: Print the car details using dot operator
    printf("Car Details:\n");
    // Print Year, Brand, Mileage, and Price here
    
    return 0;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow