struct (C+)

The struct keyword defines a structure type and/or a variable of a structure type.

[template-spec] struct [ms-decl-spec] [tag [: base-list ]]
{ 
   member-list 
} [declarators];
[struct] tag declarators;

Parameters

  • template-spec
    Optional template specifications. For more information, refer to Template Specifications.

  • struct
    The struct keyword.

  • ms-decl-spec
    Optional storage-class specification. For more information, refer to the __declspec keyword.

  • tag
    The type name given to the structure. The tag becomes a reserved word within the scope of the structure. The tag is optional. If omitted, an anonymous structure is defined. For more information, see Anonymous Class Types.

  • base-list
    Optional list of classes or structures this structure will derive its members from. See Base Classes for more information. Each base class or structure name can be preceded by an access specifier (public, private, protected) and the virtual keyword. See the member-access table in Controlling Access to Class Members for more information.

  • member-list
    List of structure members. Refer to Class Members for more information. The only difference here is that struct is used in place of class.

  • declarators
    Declarator list specifying the names of the class. Declarator lists declare one or more instances of the structure type. Declarators may include initializer lists if all data members of the class are public. Initializer lists are common in structures because data members are public by default. See Overview of Declarators for more information.

Remarks

A structure type is a user-defined composite type. It is composed of fields or members that can have different types.

In C++, a structure is the same as a class except that its members are public by default.

For information on managed classes and structs, see Classes and Structs.

Using a Structure

In C, you must explicitly use the struct keyword to declare a structure. In C++, this is unnecessary once the type has been defined.

You have the option of declaring variables when the structure type is defined by placing one or more comma-separated variable names between the closing brace and the semicolon.

Structure variables can be initialized. The initialization for each variable must be enclosed in braces.

For related information, see class, union, and enum.

Example

// struct1.cpp
struct PERSON {   // Declare PERSON struct type
   int age;   // Declare member types
   long ss;
   float weight;
   char name[25];
} family_member;   // Define object of type PERSON

int main() {
   struct PERSON sister;   // C style structure declaration
   PERSON brother;   // C++ style structure declaration
   sister.age = 13;   // assign values to members
   brother.age = 7;
}

struct POINT {   // Declare POINT structure
   int x;   // Define members x and y
   int y;
} spot = { 20, 40 };    // Variable spot has
                        // values x = 20, y = 40

struct POINT there;     // Variable there has POINT type

struct CELL {   // Declare CELL bit field
   unsigned short character  : 8;  // 00000000 ????????
   unsigned short foreground : 3;  // 00000??? 00000000
   unsigned short intensity  : 1;  // 0000?000 00000000
   unsigned short background : 3;  // 0???0000 00000000
   unsigned short blink      : 1;  // ?0000000 00000000
} screen[25][80];       // Array of bit fields 

See Also

Reference

Defining Class Types