Редагувати

Поділитися через


Declarators and variable declarations

The rest of this section describes the form and meaning of declarations for variable types summarized in this list. In particular, the remaining sections explain how to declare:

Type of Variable Description
Simple variables Single-value variables with integral or floating-point type
Arrays Variables composed of a collection of elements with the same type
Pointers Variables that point to other variables and contain variable locations (in the form of addresses) instead of values
Enumeration variables Simple variables with integral type that hold one value from a set of named integer constants
Structures Variables composed of a collection of values that can have different types
Unions Variables composed of several values of different types that occupy the same storage space

A declarator is the part of a declaration that specifies the name to introduce into the program. It can include modifiers such as * (pointer-to) and any of the Microsoft calling-convention keywords.

Microsoft Specific

In this declarator,

__declspec(thread) char *var;

char is the type specifier, __declspec(thread) and * are the modifiers, and var is the identifier name.

END Microsoft Specific

You use declarators to declare arrays of values, pointers to values, and functions returning values of a specified type. Declarators appear in the array and pointer declarations described later in this section.

Syntax

declarator:
pointeropt direct-declarator

direct-declarator:
identifier
( declarator )
direct-declarator [ constant-expressionopt ]
direct-declarator ( parameter-type-list )
direct-declarator ( identifier-listopt )

pointer:
* type-qualifier-listopt
* type-qualifier-listopt pointer

type-qualifier-list:
type-qualifier
type-qualifier-list type-qualifier

Note

See the syntax for declaration in Overview of declarations or C language syntax summary for the syntax that references a declarator.

When a declarator consists of an unmodified identifier, the item being declared has a base type. If an asterisk (*) appears to the left of an identifier, the type is modified to a pointer type. If the identifier is followed by brackets ([ ]), the type is modified to an array type. If parentheses follow the identifier, the type is modified to a function type. For more information about interpreting precedence within declarations, see Interpreting more complex declarators.

Each declarator declares at least one identifier. A declarator must include a type specifier to be a complete declaration. The type specifier gives: the type of the elements of an array type, the type of object addressed by a pointer type, or the return type of a function.

Array and pointer declarations are discussed in more detail later in this section. The following examples illustrate a few simple forms of declarators:

int list[20]; // Declares an array of 20 int values named list
char *cp;     // Declares a pointer to a char value
double func( void ); // Declares a function named func, with no
                     // arguments, that returns a double value
int *aptr[10]; // Declares an array of 10 pointers

Microsoft Specific

The Microsoft C compiler doesn't limit the number of declarators that can modify an arithmetic, structure, or union type. The number is limited only by available memory.

END Microsoft Specific

See also

Declarations and types