bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/C++/C++ Tutorial
C++•C++ Tutorial

C++ Structures (struct)

C++ Structures

Structures (also called structs) are a way to group several related variables into one place.

Each variable in the structure is known as a member of the structure.

Unlike an array , a structure can contain many different data types: int , string , bool , etc.

Create a Structure

To create a structure, use the struct keyword and declare each of its members inside curly braces.

After the declaration, specify the name of the structure variable ( myStructure in the example below):

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

Access Structure Members

To access members of a structure, use the dot syntax ( . ):

Example

// Create a structure variable called myStructure
struct {
  int myNum;
  string myString;
} myStructure;
// Assign values to members of myStructure myStructure.myNum = 1; myStructure.myString = "Hello World!"; // Print members of myStructure cout << myStructure.myNum << "\n"; cout << myStructure.myString << "\n";

One Structure in Multiple Variables

You can use a comma ( , ) to use one structure in many variables:

struct {
 int myNum;
 string myString;
} myStruct1, myStruct2, myStruct3; // Multiple structure variables separated with commas

This example shows how to use a structure in two different variables:

Example

struct {
  string brand;
  string model;
  int year;
} myCar1, myCar2;  // We can add variables by separating them with a comma here
// Put data into the first structure myCar1.brand = "BMW"; myCar1.model = "X5"; myCar1.year = 1999; // Put data into the second structure myCar2.brand = "Ford"; myCar2.model = "Mustang"; myCar2.year = 1969; // Print the structure members cout << myCar1.brand << " " << myCar1.model << " " << myCar1.year << "\n"; cout << myCar2.brand << " " << myCar2.model << " " << myCar2.year << "\n";

Named Structures

By giving a name to the structure, you can treat it as a data type. This means that you can create variables with this structure anywhere in the program at any time.

To create a named structure, put the name of the structure right after the struct keyword:

struct car { // This structure is now named "car"
 string
 brand;
 string model;
 int year;
};

To declare a variable that uses the structure, use the name of the structure as the data type of the variable:

car myCar1;

Now the structure can be reused anywhere by using car as the data type:

Example

// 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 myCar1; myCar1.brand = "BMW"; myCar1.model = "X5"; myCar1.year = 1999; // Create another car structure and store it in myCar2; car myCar2; myCar2.brand = "Ford"; myCar2.model = "Mustang"; myCar2.year = 1969; // Print the structure members cout << myCar1.brand << " " << myCar1.model << " " << myCar1.year << "\n"; cout << myCar2.brand << " " << myCar2.model << " " << myCar2.year << "\n";
  return 0;
}

Challenge Task

Create a structure to represent a student. It should have the following members:

  • name ( string )
  • age ( int )
  • grade ( char )

Then

  • Create one variable of the structure.
  • Assign values to its members.
  • Print the values to the screen.
Name: Liam
Age: 35
Grade: A

Solution

Runnable example

#include <iostream>
#include <string>
using namespace std;
struct student {
  string name;
  int age;
  char grade;
};
int main() {
  student s1;
  s1.name = "John";
  s1.age = 35;
  s1.grade = 'A';
  cout << "Name: " << s1.name << "\n";
  cout << "Age: " << s1.age << "\n";
  cout << "Grade: " << s1.grade << "\n";
  return 0;
}

Previous

C++ Multi-Dimensional Arrays

Next

C++ Enumeration (enum)

This chapter

Overview
64

Lessons

138m

Read time

1. C++ Tutorial2. C++ Introduction3. C++ Getting Started4. C++ Syntax5. C++ Statements6. C++ Output (Print Text)7. C++ Output Numbers8. C++ New Lines9. C++ Comments10. C++ Variables11. C++ Declare Multiple Variables12. C++ Identifiers13. C++ Constants14. C++ User Input15. C++ Data Types16. C++ Numeric Data Types17. C++ Boolean Data Types18. C++ Character Data Types19. C++ String Data Types20. C++ auto21. C++ Operators22. C++ Arithmetic Operators23. C++ Assignment Operators24. C++ Comparison Operators25. C++ Logical Operators26. C++ Operator Precedence27. C++ Strings28. C++ String Concatenation29. C++ Numbers and Strings30. C++ String Length31. C++ Access Strings32. C++ Special Characters33. C++ User Input Strings34. C++ String Namespace35. C++ C-Style Strings36. C++ Math37. C++ Booleans38. C++ Boolean Expressions39. C++ If ... Else40. C++ Else41. C++ Else If42. C++ Short Hand If Else43. C++ Nested If44. C++ Logical Operators in Conditions45. C++ Switch46. C++ While Loop47. C++ Do/While Loop48. C++ For Loop49. C++ Nested Loops50. C++ The foreach Loop51. C++ Break and Continue52. C++ Arrays53. C++ Arrays and Loops54. C++ Omit Array Size55. C++ Array Size56. C++ Multi-Dimensional Arrays57. C++ Structures (struct)58. C++ Enumeration (enum)59. C++ Memory Address60. C++ Pointers61. C++ Dereference62. C++ Modify Pointers63. C++ Memory Management64. C++ new and delete

On this page

C++ StructuresCreate a StructureAccess Structure MembersOne Structure in Multiple VariablesNamed StructuresChallenge TaskSolution