download > pdf > do ÂściÂągnięcia > pobieranie > ebook

[ Pobierz całość w formacie PDF ]

v  Conditional Expressions on page 109
v  Function Declarations on page 124
Structures
A structure contains an ordered group of data objects. Unlike the elements
of an array, the data objects within a structure can have varied data types. Each
data object in a structure is a member or field.
Use structures to group logically related objects. For example, to allocate storage
for the components of one address, define the following variables:
int street_no;
char *street_name;
char *city;
char *prov;
char *postal_code;
To allocate storage for more than one address, group the components of each
address by defining a structure data type and as many variables as you need to
have the structure data type.
In the following example, lineintstreet_no;through tochar*postal_code;
declare the structure tagaddress:
struct address {
int street_no;
char *street_name;
char *city;
char *prov;
char *postal_code;
};
struct address perm_address;
struct address temp_address;
struct address *p_perm_address = &perm_address;
The variablesperm_addressandtemp_addressare instances of the structure data
typeaddress. Both contain the members described in the declaration ofaddress.
The pointerp_perm_addresspoints to a structure ofaddressand is initialized to
point toperm_address.
Refer to a member of a structure by specifying the structure variable name with
the dot operator (.) or a pointer with the arrow operator (->) and the member
name. For example, both of the following:
perm_address.prov = "Ontario";
p_perm_address -> prov = "Ontario";
assign a pointer to the string"Ontario"to the pointerprovthat is in the structure
perm_address.
36 C/C++ Language Reference
Type Specifiers
All references to structures must be fully qualified. In the example, you cannot
reference the fourth field byprovalone. You must reference this field by
perm_address.prov.
Structures with identical members but different names are not compatible and
cannot be assigned to each other.
Structures are not intended to conserve storage. If you need direct control of byte
mapping, use pointers.
You cannot declare a structure with members of incomplete types.
In C++ a structure is the same as a class except that its members and
inheritance are public by default.
v  Chapter 12. Classes on page 199
v  Dot Operator . on page 81
v  Arrow Operator -> on page 82
v  Incomplete Types on page 51
Declaring and Defining a Structure
A structure type definition describes the members that are part of the
structure. It contains the struct keyword followed by an optional identifier (the
structure tag) and a brace-enclosed list of members.
A structure definition has the form:
struct identifier
{ member ; }
identifier
A structure declaration has the same form as a structure definition except the
declaration does not have a brace-enclosed list of members.
The keyword struct followed by the identifier (tag) names the data type. If you do
not provide a tag name to the data type, you must put all variable definitions that
refer to it within the declaration of the data type.
The list of members provides the data type with a description of the values that
can be stored in the structure.
A structure data member definition has the form of a variable declaration.
However you may declare a bit-field as a member for a structure. A member that
does not represent a bit field can be of any data type and can have the volatile or
const qualifier.
Identifiers used as structure or member names can be redefined to represent
different objects in the same scope without conflicting. You cannot use the name of
a member more than once in a structure type, but you can use the same member
name in another structure type that is defined within the same scope.
Chapter 3. Declarations 37
Type Specifiers
You cannot declare a structure type that contains itself as a member, but you can
declare a structure type that contains a pointer to itself as a member.
v  Declaring and Using Bit Fields in Structures on page 39
v  volatile and const Qualifiers on page 50
Defining a Structure Variable
A structure variable definition contains an optional storage class keyword,
the struct keyword, a structure tag, a declarator, and an optional identifier. The
structure tag indicates the data type of the structure variable.
The keyword struct is optional in C++.
You can declare structures having any storage class. Most compilers, however, treat
structures declared with the register storage class specifier as automatic structures.
v  auto Storage Class Specifier on page 25
v  register Storage Class Specifier on page 27
Initializing Structures
The initializer contains an=(equal sign) followed by a brace-enclosed
comma-separated list of values. You do not have to initialize all members of a
structure.
The following definition shows a completely initialized structure:
struct address {
int street_no;
char *street_name;
char *city;
char *prov;
char *postal_code;
};
static struct address perm_address =
{ 3, "Savona Dr.", "Dundas", "Ontario", "L4B 2A1"};
The values ofperm_addressare:
Member Value
perm_address.street_no 3
perm_address.street_name address of string"SavonaDr."
perm_address.city address of string"Dundas"
perm_address.prov address of string"Ontario"
perm_address.postal_code address of string"L4B2A1"
The following definition shows a partially initialized structure:
struct address {
int street_no;
char *street_name;
char *city;
char *prov;
char *postal_code;
};
struct address temp_address =
{ 44, "Knyvet Ave.", "Hamilton", "Ontario" };
The values oftemp_addressare:
38 C/C++ Language Reference
Type Specifiers
Member Value
temp_address.street_no 44
temp_address.street_name address of string"KnyvetAve."
temp_address.city address of string"Hamilton"
temp_address.prov address of string"Ontario"
temp_address.postal_code value depends on the storage class.
Note: The initial value of uninitialized structure members like
temp_address.postal_codedepends on the storage class associated with the
member.
Declaring Structure Types and Variables
To define a structure type and a structure variable in one statement, put a
declarator and an optional initializer after the type definition. To specify a storage
class specifier for the variable, you must put the storage class specifier at the
beginning of the statement.
For example:
static struct {
int street_no;
char *street_name;
char *city;
char *prov;
char *postal_code;
} perm_address, temp_address;
Because this example does not name the structure data type,perm_addressand
temp_addressare the only structure variables that will have this data type. Putting
an identifier after struct, lets you make additional variable definitions of this data
type later in the program.
The structure type (or tag) cannot have the volatile qualifier, but a member or a
structure variable can be defined as having the volatile qualifier.
For example:
static struct class1 {
char descript[20];
volatile long code;
short complete;
} volatile file1, file2;
struct class1 subfile;
This example qualifies the structuresfile1andfile2, and the structure member
subfile.codeas volatile.
v  Initializing Structures on page 38
v  Storage Class Specifiers on page 24
v  volatile and const Qualifiers on page 50
Declaring and Using Bit Fields in Structures
A structure or a C++ class can contain bit fields that allow you to
access individual bits. You can use bit fields for data that requires just a few bits of [ Pobierz całość w formacie PDF ]
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • aikidobyd.xlx.pl
  •