#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// -----------------------------
// Simple nested struct (C style)
// -----------------------------
typedef struct {
char street[64];
char city[64];
int zip;
} Address;
// -----------------------------
// More complex struct
// -----------------------------
typedef struct {
Address homeAddress; // Nested struct
int favoriteNumbers[5]; // Fixed-size C array
char* nickname; // Raw pointer
// C has no chrono::year_month_day, so we use a simple struct
struct {
int year;
int month;
int day;
} birthDate;
char* notes; // C has no smart pointers → raw char*
char* middleName; // C has no optional → NULL means "none"
char** hobbies; // C has no vector → dynamic array of char*
size_t hobbyCount;
// C enum (no enum class)
enum Status { Active, Inactive, Unknown } status;
} Person;
// -----------------------------
// Print function (C style)
// -----------------------------
void printPerson(const Person* p) {
printf("Address: %s, %s %d\n",
p->homeAddress.street,
p->homeAddress.city,
p->homeAddress.zip);
printf("Favorite Numbers: ");
for (int i = 0; i < 5; i++)
printf("%d ", p->favoriteNumbers[i]);
printf("\n");
printf("Nickname: %s\n", p->nickname ? p->nickname : "(none)");
printf("Birth Date: %04d-%02d-%02d\n",
p->birthDate.year,
p->birthDate.month,
p->birthDate.day);
printf("Notes: %s\n", p->notes ? p->notes : "(none)");
printf("Middle Name: %s\n", p->middleName ? p->middleName : "(none)");
printf("Hobbies: ");
for (size_t i = 0; i < p->hobbyCount; i++)
printf("%s, ", p->hobbies[i]);
printf("\n");
printf("Status: ");
switch (p->status) {
case Active: printf("Active"); break;
case Inactive: printf("Inactive"); break;
default: printf("Unknown"); break;
}
printf("\n");
}
// -----------------------------
// Main (C style initialization)
// -----------------------------
int main() {
Person p;
// Initialize nested struct
strcpy(p.homeAddress.street, "Fifth Avenue");
strcpy(p.homeAddress.city, "New York");
p.homeAddress.zip = 423900;
// Initialize array
int favs[5] = {7, 13, 21, 42, 99};
memcpy(p.favoriteNumbers, favs, sizeof(favs));
// Initialize nickname
p.nickname = malloc(10);
snprintf(p.nickname, 10, "Orion");
// Initialize date
p.birthDate.year = 1026;
p.birthDate.month = 6;
p.birthDate.day = 15;
// Initialize notes
p.notes = malloc(64);
strcpy(p.notes, "Voss likes C/C++ and structs");
// Initialize optional middle name
p.middleName = malloc(32);
strcpy(p.middleName, "Pulse‑9");
// Initialize hobbies (dynamic array of char*)
p.hobbyCount = 4;
p.hobbies = malloc(sizeof(char*) * p.hobbyCount);
p.hobbies[0] = strdup("Coding");
p.hobbies[1] = strdup("Dreaming");
p.hobbies[2] = strdup("Walking");
p.hobbies[3] = strdup("Sci-Fi Movies");
// Set enum value
p.status = Active;
// Print everything
printPerson(&p);
// Cleanup
free(p.nickname);
free(p.notes);
free(p.middleName);
for (size_t i = 0; i < p.hobbyCount; i++)
free(p.hobbies[i]);
free(p.hobbies);
return 0;
}
/*
run:
Address: Fifth Avenue, New York 423900
Favorite Numbers: 7 13 21 42 99
Nickname: Orion
Birth Date: 1026-06-15
Notes: Voss likes C/C++ and structs
Middle Name: Pulse‑9
Hobbies: Coding, Dreaming, Walking, Sci-Fi Movies,
Status: Active
*/