3.5 Declarators and Variable Declarations

The rest of this chapter 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 and memory-model keywords.

Microsoft Specific

In the declarator

char * __far *var;

char is the type specifier, *__far and * are the modifiers, and var is the identifier's name.¨

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

Syntax

declarator :
pointer opt direct-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 opt pointer

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

Note:

See the syntax for declaration in “Overview of Declarations”, or look in Appendix A 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. See topic for more information about interpreting precedence within declarations.

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.

Microsoft Specific

Up to 12 pointer, array, and function declarators in any valid combination are allowed to modify an arithmetic, structure, union, or incomplete type either directly or with typedefdeclarations.¨Array, pointer, and function declarations are discussed in more detail later in this chapter. The following examples illustrate a few simple forms of declarators:

int list[20]; /* Declares an array of int values named list */

char *cp; /* Declares a pointer named cp to a char value */

double func( void ); /* Declares a function named func, with no

arguments, that returns a double value */

int *aptr[1] /* 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.¨