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.memberNameUsing 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
EasyCreate a C program that demonstrates accessing and modifying struct members using the dot operator. Your program should:
- Define a
structnamedCarwith the following members:- An integer
yearto store the manufacturing year - A character array
brandwith size 20 to store the car brand - An integer
mileageto store the current mileage - A float
priceto store the car's price
- An integer
- Create a
Carvariable namedmyCar - 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
- Read an integer for the year and assign it to
- 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;
- Increase the mileage by 1000 using
- 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.50Your output should be:
Car Details:
Year: 2020
Brand: Toyota
Mileage: 51000
Price: 24500.50Cheat sheet
The dot operator (.) is used to access and modify individual members of a struct variable.
Syntax for accessing struct members:
structVariable.memberNameExample 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;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Pointers Fundamentals
What is a Pointer?Declaring PointersThe Address-Of Operator (&)The Dereference Operator (*)NULL PointersRecap: Pointer Basics4Project: Simple Text Utility
Project OverviewCounting Characters7Structures (structs)
What is a Struct?Declaring a StructCreating Struct VariablesAccessing Struct MembersInitializing StructsRecap: Student Data Struct2Pointers and Arrays
Array Names as PointersArray Elements - PointersPointer ArithmeticComparing PointersRecap: Pointer Array Traversal5Pointers and Functions
Pass-by-ValuePassing Pointers to FunctionsModifying Vars via PointersA Classic Example: SwapPassing Arrays to FunctionsRecap: Function Pointer Args8Structs and Pointers
Pointers to StructsThe Arrow Operator (->)Passing Structs by ValuePassing Struct PointersDynamic Allocation of StructsRecap: Modifying Struct - Ptr11Final Recap Challenges
Recap: Dynamic String ConcatRecap: Array of StructsRecap: Word Frequency Counter3Character Arrays and Strings
Strings as char ArraysThe Null TerminatorString Input with scanfUsing strlen()Using strcpy()Using strcat()Using strcmp()Recap: Basic String Functions