A structure is a collection of variables referenced under one name, providing a
convenient means of keeping related information together. A structure declaration
forms a template that may be used to create structure objects (that is, instances of
a structure). The variables that make up the structure are called members. (Structure
members are also commonly referred to as elements or fields.)
Generally, all of the members of a structure are logically related. For example, the
name and address information in a mailing list would normally be represented in a
structure. The following code fragment shows how to declare a structure that defines
the name and address fields. The keyword struct tells the compiler that a structure is
being declared.
struct addr
{
char name[30];
char street[40];
char city[20];
char state[3];
unsigned long int zip;
};
convenient means of keeping related information together. A structure declaration
forms a template that may be used to create structure objects (that is, instances of
a structure). The variables that make up the structure are called members. (Structure
members are also commonly referred to as elements or fields.)
Generally, all of the members of a structure are logically related. For example, the
name and address information in a mailing list would normally be represented in a
structure. The following code fragment shows how to declare a structure that defines
the name and address fields. The keyword struct tells the compiler that a structure is
being declared.
struct addr
{
char name[30];
char street[40];
char city[20];
char state[3];
unsigned long int zip;
};
Notice that the declaration is terminated by a semicolon. This is because a structure
declaration is a statement. The type name of the structure is addr. As such, addr
identifies this particular data structure and is its type specifier.
At this point, no variable has actually been created. Only the form of the data has been
defined. When you define a structure, you are defining a compound variable type, not
a variable. Not until you declare a variable of that type does one actually exist. In C, to
declare a variable (i.e., a physical object) of type addr, write
struct addr addr_info;
This declares a variable of type addr called addr_info. In C++, you may use this shorter
form.
addr addr_info;
As you can see, the keyword struct is not needed. In C++, once a structure
has been declared, you may declare variables of its type using only its type name,
without preceding it with the keyword struct. The reason for this difference is that
in C, a structure's name does not define a complete type name. In fact, Standard C
refers to a structure's name as a tag. In C, you must precede the tag with the keyword
struct when declaring variables. However, in C++, a structure's name is a complete
type name and may be used by itself to define variables. Keep in mind, however,
that it is still perfectly legal to use the C-style declaration in a C++ program. Since the
programs in Part One of this book are valid for both C and C++, they will use the C
declaration method. Just remember that C++ allows the shorter form.
When a structure variable (such as addr_info) is declared, the compiler automatically
allocates sufficient memory to accommodate all of its members. Figure 7-1
shows how addr_info appears in memory assuming 1-byte characters and 4-byte
long integers.
You may also declare one or more structure variables when you declare a structure.
For example,
struct addr {
char name[30];
char street[40];
char city[20];
char state[3];
unsigned long int zip;
} addr_info, binfo, cinfo;
defines a structure type called addr and declares variables addr_info, binfo, and cinfo
of that type.
declaration is a statement. The type name of the structure is addr. As such, addr
identifies this particular data structure and is its type specifier.
At this point, no variable has actually been created. Only the form of the data has been
defined. When you define a structure, you are defining a compound variable type, not
a variable. Not until you declare a variable of that type does one actually exist. In C, to
declare a variable (i.e., a physical object) of type addr, write
struct addr addr_info;
This declares a variable of type addr called addr_info. In C++, you may use this shorter
form.
addr addr_info;
As you can see, the keyword struct is not needed. In C++, once a structure
has been declared, you may declare variables of its type using only its type name,
without preceding it with the keyword struct. The reason for this difference is that
in C, a structure's name does not define a complete type name. In fact, Standard C
refers to a structure's name as a tag. In C, you must precede the tag with the keyword
struct when declaring variables. However, in C++, a structure's name is a complete
type name and may be used by itself to define variables. Keep in mind, however,
that it is still perfectly legal to use the C-style declaration in a C++ program. Since the
programs in Part One of this book are valid for both C and C++, they will use the C
declaration method. Just remember that C++ allows the shorter form.
When a structure variable (such as addr_info) is declared, the compiler automatically
allocates sufficient memory to accommodate all of its members. Figure 7-1
shows how addr_info appears in memory assuming 1-byte characters and 4-byte
long integers.
You may also declare one or more structure variables when you declare a structure.
For example,
struct addr {
char name[30];
char street[40];
char city[20];
char state[3];
unsigned long int zip;
} addr_info, binfo, cinfo;
defines a structure type called addr and declares variables addr_info, binfo, and cinfo
of that type.
If you only need one structure variable, the structure type name is not needed. That
means that
struct {
char name[30];
char street[40];
char city[20];
char state[3];
unsigned long int zip;
} addr_info;
declares one variable named addr_info as defined by the structure preceding it.
The general form of a structure declaration is
struct struct-type-name {
type member-name;
type member-name;
type member-name;
.. .
} structure-variables;
where either struct-type-name or structure-variables may be omitted, but not both.
means that
struct {
char name[30];
char street[40];
char city[20];
char state[3];
unsigned long int zip;
} addr_info;
declares one variable named addr_info as defined by the structure preceding it.
The general form of a structure declaration is
struct struct-type-name {
type member-name;
type member-name;
type member-name;
.. .
} structure-variables;
where either struct-type-name or structure-variables may be omitted, but not both.
No comments:
Post a Comment