A declaration is a list of names. The names are sometimes referred to as declarators or identifiers. The declaration begins with optional storage class specifiers, type specifiers, and other modifiers. The identifiers are separated by commas and the list is terminated by a semicolon.
Simple declarations of variable identifiers have the following pattern:
data-type var1 <=init1>, var2 <=init2>, ...;
where var1, var2,... are any sequence of distinct identifiers with optional initializers. Each of the variables is declared to be of type data-type. For example:
int x = 1, y = 2;
creates two integer variables called x and y (and initializes them to the values 1 and 2, respectively).
These are all defining declarations; storage is allocated and any optional initializers are applied.
The initializer for an automatic object can be any legal expression that evaluates to an assignment-compatible value for the type of the variable involved. Initializers for static objects must be constants or constant expressions.
In C++, an initializer for a static object can be any expression involving constants and previously declared variables and functions.
The format of the declarator indicates how the declared name is to be interpreted when used in an expression. If type is any type, and storage class specifier is any storage class specifier, and if D1 and D2 are any two declarators, then the declaration:
storage-class-specifier type D1, D2;
indicates that each occurrence of D1 or D2 in an expression will be treated as an object of type type and storage class storage class specifier. The type of the name embedded in the declarator will be some phrase containing type, such as "type," "pointer to type," "array of type," "function returning type," or "pointer to function returning type," and so on.
For example, in Declaration syntax examples each of the declarators could be used as rvalues (or possibly lvalues in some cases) in expressions where a single int object would be appropriate. The types of the embedded identifiers are derived from their declarators as follows:
Declaration syntax examples
Declarator syntax |
Implied type of name |
Example |
type |
name; type |
int count; |
type name[]; |
(open) array of type |
int count[]; |
type name[3]; |
Fixed array of three elements, int count[3] all of type; |
(name[0], name[1], and name[2] |
type *name; |
Pointer to type |
int *count; |
type *name[]; |
(open) array of pointers to type |
int *count[]; |
type *(name[]); |
Same as above |
int *(count[]); |
type (*name)[]; |
Pointer to an (open) array of type |
int (*count) []; |
type &name; |
Reference to type (C++ only) |
int &count; |
type name(); |
Function returning type |
int count(); |
type *name(); |
Function returning pointer to type |
int *count(); |
type *(name()); |
Same as above |
int *(count()); |
type (*name)(); |
Pointer to function returning type |
int (*count) (); |
Note the need for parentheses in (*name)[ ] and (*name)( ); this is because the precedence of both the array declarator [ ] and the function declarator ( ) is higher than the pointer declarator *. The parentheses in *(name[ ]) are optional.
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|