Variables

Structures in C++

A structure stores several data items and variables.


Syntax
struct structureName { 
//member declarations 
};
 
//to access a structure member
struct structureName structureInstanceName; //creating a structure instance
givenName.member; //standard members
givenName->member; //pointer members

Notes

Structures can be passed as function arguments (using the struct data type).
Structures must be declared as an instance to be accessed.
Structures can be pointers, similar to variables.

To avoid having to type out 'struct' all the time, the typedef keyword can be used when declaring the structure. This way, 'struct' only needs to be used when declaring the structure, and can be omitted when creating an instance of it.


Example
struct Car { 
    char make[50]; 
    char model[50]; 
    int year; 
} 
 
struct Car myCar;
myCar.year = 2014;

See Also
Related

Local Variables

Documentation

Data structures - C++ Tutorials

Add to Slack

< Pointers   |   Type Definition >

© 2019 SyntaxDB. All Rights Reserved.