C++ Structures (struct) | Examples

Multiple related variables can be gathered into a single entity using structures (also known as structs). The structure’s variables collectively are referred to as its members.

A structure, as opposed to an array, can hold a wide variety of data types (such as int, string, bool, etc.).


Create a Structure

Use the structkeyword to create a structure, and then enclose each member’s declaration within curly braces.

The structure variable’s name (geektocode in the example below) should be specified after the declaration:

struct {             // Structure declaration
  int myNum;         // Member (int variable)
  string myString;   // Member (string variable)
} geektocode;       // Structure variable

Access Structure Members

Use the dot syntax (.) to access a structure’s members:

Example:

#include <iostream>
#include <string>
using namespace std;

int main() {
  struct {
    int myNum;
    string myString;
  } vargeek;

  vargeek.myNum = 2023;
    vargeek.myString = "GeektoCode!";

  cout <<   vargeek.myNum << "\n";
  cout <<   vargeek.myString << "\n";
  return 0;
}
OUTPUT:
2023
GeektoCode!

Single Structure in Multiple Variables

A comma (,) can be used to use a single structure across multiple variables:

Syntax:

struct {
  int myNum;
  string myString;
} geek1, geek2, geek3; // Multiple structure variables separated with commas

Example:

#include <iostream>
#include <string>
//geektocode.com
using namespace std;

int main() {
  struct {
    string brand;
    string model;
    int year;
  } Car1, Car2; // We can add variables by separating them with a comma here

  // Put data into the first structure
  Car1.brand = "AUDI";
  Car1.model = "X10";
  Car1.year = 2022;

  // Put data into the second structure
  Car2.brand = "Ford";
  Car2.model = "Mustang";
  Car2.year = 1969;

  // Print the structure members
  cout << Car1.brand << " " << Car1.model << " " << Car1.year << "\n";
  cout << Car2.brand << " " << Car2.model << " " << Car2.year << "\n";
  return 0;
}
OUTPUT:
AUDI X10 2022
Ford Mustang 1969

Named Structures

You can treat the structure as a data type by giving it a name. This implies that you can create variables using this structure at any point during the course of the program.

Example:

#include <iostream>
#include <string>
//geektocode.com
using namespace std;

// Declare a structure named "car"
struct car {
  string brand;
  string model;
  int year;
};

int main() {
  // Create a car structure and store it in myCar1;
  car Car1;
  Car1.brand = "BMW";
  Car1.model = "X5";
  Car1.year = 1999;

  // Create another car structure and store it in myCar2;
  car Car2;
  Car2.brand = "Ford";
  Car2.model = "Mustang";
  Car2.year = 1969;
 
  // Print the structure members
  cout << Car1.brand << " " << Car1.model << " " << Car1.year << "\n";
  cout << Car2.brand << " " << Car2.model << " " << Car2.year << "\n";
 
  return 0;
}
OUTPUT:
BMW X5 1999
Ford Mustang 1969

Leave a Reply

Your email address will not be published. Required fields are marked *