I recently took on a small C programming project to better understand structures, function pointers, and how to manipulate and display data efficiently. The task was to create a data structure representing a video game, populate it with values, and then print the game details using a function.
Seems simple at first, right? Well, like many things in programming, the devil was in the details. Let me walk you through what I did, what went wrong, how I fixed it, and how I expanded it to include more practical features.
What I Set Out to Do
I wanted to:
- Create a
Video Game
structure with attributes liketitle
,genre
,platform
,developer
,year
,price
, and so on. - Declare three games:
Candy Crush Saga
,Halo 4
, and one of my personal favorites:Genshin Impact
. - Write a function to print game details by passing in a pointer to the structure.
- Call the print function from
main()
for each game.
Easy in theory. But my first attempt? Not quite there.
What Went Wrong
As a beginner, I made some classic C mistakes:
Data Type Mismatches
I assigned string values like "2012"
and "$0.00"
to integer and float variables. C is strict about types. Instead, I should have used:
int year = 2012;
float price = 0.00;
Incorrect Looping
I wrote a loop using game[i]
expecting an array of games — but I only had separate variables (game1
, game2
, game3
). Without an array, that loop made no sense.
Function Declaration Mistake
I declared my print_video_game_details()
function with no parameters but tried using game[i]
inside it. Again, a mistake. I needed to pass a pointer to the struct like this:
void print_video_game_details(struct Video_Game* game);
Typos and Spaces in Variable Name
I tried to access game[i].lower age
, which C completely rejected variable names can’t have spaces. I fixed it to lower_age
.
Case Sensitivity Confusion
C is case-sensitive. I defined struct video_game
but tried to use Video_Game
. I cleaned it up and consistently used Video_Game
.
The Fix and Functional Code
Here’s what my corrected and working code looked like:
#include <stdio.h>
#include <string.h>
// Define the structure
struct Video_Game {
char* title;
char* genre;
char* developer;
int year;
char* platform;
int lower_age;
float price;
char* inapp_purchase;
};
// Print details function
void print_video_game_details(struct Video_Game* game) {
printf("Title: %s\n", game->title);
printf("Genre: %s\n", game->genre);
printf("Developer: %s\n", game->developer);
printf("Year of Release: %d\n", game->year);
printf("Platform: %s\n", game->platform);
printf("Lower Age Limit: %d\n", game->lower_age);
printf("Price: $%.2f\n", game->price);
printf("In-App Purchases: %s\n", game->inapp_purchase);
printf("\n");
}
int main(void) {
// Initialize games
struct Video_Game game1 = {
"Candy Crush Saga", "Match-Three Puzzle", "King", 2012,
"Android, iOS, Windows Phone", 7, 0.00, "Yes"
};
struct Video_Game game2 = {
"Halo 4", "First-Person Shooter", "343 Industries", 2014,
"Xbox 360", 16, 59.99, "No"
};
struct Video_Game game3 = {
"Genshin Impact", "Action RPG", "miHoYo", 2020,
"PC, PS4, PS5, Android, iOS", 12, 0.00, "Yes"
};
// Print them
print_video_game_details(&game1);
print_video_game_details(&game2);
print_video_game_details(&game3);
return 0;
}
And boom it finally worked like a charm!
Extra Feature I Added for Practice
After getting the basics working, I thought, “Why stop here?” So I added a few more useful functions to play with the data:
Compare Two Games by Price
void compare_prices(struct Video_Game* g1, struct Video_Game* g2) {
if (g1->price > g2->price)
printf("%s is more expensive than %s.\n", g1->title, g2->title);
else if (g1->price < g2->price)
printf("%s is cheaper than %s.\n", g1->title, g2->title);
else
printf("%s and %s have the same price.\n", g1->title, g2->title);
}
Search Games by Genre
void search_by_genre(struct Video_Game games[], int size, char* genre) {
printf("Games in genre '%s':\n", genre);
for (int i = 0; i < size; i++) {
if (strcmp(games[i].genre, genre) == 0) {
print_video_game_details(&games[i]);
}
}
}
Loop Through an Array of Games
struct Video_Game games[3] = {game1, game2, game3};
for (int i = 0; i < 3; i++) {
print_video_game_details(&games[i]);
}
Final Thought
This project taught me a lot not just about structures, but about the importance of paying attention to syntax, data types, and function design in C. It’s easy to get stuck, but also incredibly satisfying to debug and improve your code.