Basic_Struct_Programs
Basic_Struct_Programs
**Code:**
#include <iostream>
struct Student {
string name;
int age;
float marks;
};
int main() {
Student s1;
s1.name = "John";
s1.age = 20;
s1.marks = 85.5;
return 0;
Page 1
Structs Practice Programs with Explanations
**Explanation:**
This program defines a `Student` struct with three members: `name`, `age`, and `marks`. It then
creates an instance of `Student`, assigns values to its members, and prints them.
**Code:**
#include <iostream>
struct Student {
string name;
int age;
float marks;
};
int main() {
Student students[3];
Page 2
Structs Practice Programs with Explanations
cout << "Student " << i + 1 << ": " << students[i].name << ", Age: " << students[i].age << ",
return 0;
**Explanation:**
This program defines a `Student` struct and creates an array of `Student` instances. It assigns
values to the elements of the array and then prints the details of each student using a for loop.
Nested Structs
**Code:**
#include <iostream>
struct Address {
string city;
int zip;
};
struct Student {
Page 3
Structs Practice Programs with Explanations
string name;
int age;
float marks;
Address address;
};
int main() {
Student s1;
s1.name = "John";
s1.age = 20;
s1.marks = 85.5;
s1.address.zip = 10001;
return 0;
**Explanation:**
Page 4
Structs Practice Programs with Explanations
This program demonstrates nested structs by defining an `Address` struct and including it as a
member of the `Student` struct. It assigns values to the nested struct members and prints them.
Page 5