Freigeben über


Declarators and Variable Declarations

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at 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 the following:

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 that is to be introduced into the program. It can include modifiers such as * (pointer-to) and any of the Microsoft calling-convention keywords.

Microsoft Specific

In the declarator

__declspec(thread) char *var;  

char is the type specifier, __declspec(thread) and * are the modifiers, and var is the identifier's 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:
pointer optdirect-declarator

direct-declarator:
identifier

(  declarator  )

direct-declarator  [  constant-expression opt\ ]

direct-declarator  (  parameter-type-list  )

direct-declarator  (  identifier-list opt )

pointer:
* type-qualifier-list opt

* type-qualifier-list optpointer

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 the identifier is followed by parentheses, 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 does not 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