11/11/2024, 17:28 C++ Structures (struct)
Tutorials Exercises Services My W3Schools
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
C++ Structures (struct)
❮ Previous Next ❯
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
https://www.w3schools.com/cpp/cpp_structs.asp 1/9
11/11/2024, 17:28 C++ Structures (struct)
To access members of a structure, use the dot syntax ( . ):
Tutorials Exercises Services My W3Schools
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Example
Assign data to members of a structure and print it:
// 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";
Try it Yourself »
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
Use one structure to represent two cars:
https://www.w3schools.com/cpp/cpp_structs.asp 2/9
11/11/2024, 17:28 C++ Structures (struct)
struct {
Tutorials
string brand;
Exercises Services My W3Schools
string model;
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
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";
Try it Yourself »
ADVERTISEMENT
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.
https://www.w3schools.com/cpp/cpp_structs.asp 3/9
11/11/2024, 17:28 C++ Structures (struct)
To create a named structure, put the name of the structure right after the struct
keyword:
Tutorials Exercises Services My W3Schools
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
struct myDataType { // This structure is named "myDataType"
int myNum;
string myString;
};
To declare a variable that uses the structure, use the name of the structure as the
data type of the variable:
myDataType myVar;
Example
Use one structure to represent two cars:
// 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 <<
https://www.w3schools.com/cpp/cpp_structs.asp 4/9
11/11/2024, 17:28 C++ Structures (struct)
"\n";
coutTutorials Exercises
<< myCar2.brand Services
<< " " <<
myCar2.model << My W3Schools
" " << myCar2.year <<
"\n";
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
return 0;
}
Try it Yourself »
❮ Previous Next ❯
ADVERTISEMENT
https://www.w3schools.com/cpp/cpp_structs.asp 5/9
11/11/2024, 17:28 C++ Structures (struct)
Tutorials Exercises Services My W3Schools
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
COLOR PICKER
https://www.w3schools.com/cpp/cpp_structs.asp 6/9
11/11/2024, 17:28 C++ Structures (struct)
Tutorials Exercises Services My W3Schools
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
ADVERTISEMENT
ADVERTISEMENT
https://www.w3schools.com/cpp/cpp_structs.asp 7/9
11/11/2024, 17:28 C++ Structures (struct)
Tutorials Exercises Services My W3Schools
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
ADVERTISEMENT
PLUS SPACES GET CERTIFIED
FOR TEACHERS FOR BUSINESS CONTACT US
Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial
Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
W3.CSS Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference
Top Examples Get Certified
https://www.w3schools.com/cpp/cpp_structs.asp 8/9
11/11/2024, 17:28 C++ Structures (struct)
HTML Examples HTML Certificate
CSS Examples
Tutorials Exercises
JavaScript Examples
Services CSS Certificate
JavaScript Certificate
My W3Schools
How To Examples Front End Certificate
HTML
CSS SQL Examples
JAVASCRIPT SQL PYTHON SQL CertificatePHP
JAVA HOW TO W3.CSS C
Python Examples Python Certificate
W3.CSS Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate
FORUM ABOUT ACADEMY
W3Schools is optimized for learning and training. Examples might be simplified to
improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we
cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our
terms of use, cookie and privacy policy.
Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by
W3.CSS.
https://www.w3schools.com/cpp/cpp_structs.asp 9/9