C++ Structures (Struct)
C++ Structures (Struct)
JAVA
PHP HOW TO W3.CSS C C++ C
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.
Create a Structure
To create a structure, use the struct keyword
and declare each of its members inside curly
braces.
struct { // Structure
declaration
int myNum; // Member (int
variable)
string myString; // Member (string
variable)
} myStructure; // Structure
variable
Example
Assign data to members of a structure and print
it:
Try it Yourself »
struct {
int myNum;
string myString;
} myStruct1, myStruct2, myStruct3; //
Multiple structure variables separated
with commas
Example
Use one structure to represent two cars:
struct {
string brand;
string model;
int year;
} myCar1, myCar2; // We can add
variables by separating them with a
comma here
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.
myDataType myVar;
Example
Use one structure to represent two cars:
int main() {
// Create a car structure and store
it in myCar1;
car myCar1;
myCar1.brand = "BMW";
myCar1.model = "X5";
myCar1.year = 1999;
return 0;
}
Try it Yourself »
Exercise ?
An array of integers
Submit Answer »
‹ Previous Next ›
Sign Up Log in
ADVERTISEMENT
COLOR PICKER
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
HTML Examples
CSS Examples
JavaScript Examples
How To Examples
SQL Examples
Python Examples
W3.CSS Examples
Bootstrap Examples
PHP Examples
Java Examples
XML Examples
jQuery Examples
Get Certified
HTML Certificate
CSS Certificate
JavaScript Certificate
Front End Certificate
SQL Certificate
Python Certificate
PHP Certificate
jQuery Certificate
Java Certificate
C++ Certificate
C# Certificate
XML Certificate