Jump to 
content
HP.com Home Products and Services Support and Drivers Solutions How to Buy
»  Contact HP

 

dec_c_help.HLP

Array

An array is an aggregate of subscripted elements of the same type. Elements of an array can have one of the fundamental data types or can be structures, unions, or other arrays (multidimensional arrays). An array is declared using square brackets. The following example declares array1 to be an array of 10 integers. The valid subscripts for array1 range from 0 to 9. int array1[10]; The next example declares array2 to be a two-dimensional (2 by 3) array of integers: int array2[2][3]; The elements are stored in row-major order as follows: array2[0][0], array2[0][1], ... array2[1][2].

enum

An enumerated type is used to restrict the possible values of an object to a predefined list. Elements of the list are called enumeration constants. The main use of enumerated types is to explicitly show the symbolic names, and therefore the intended purpose, of objects that can be represented with integer values. Objects of enumerated type are interpreted as objects of type signed int, and are compatible with objects of other integral types. The compiler automatically assigns integer values to each of the enumeration constants, beginning with 0. An enumerated type is a set of scalar objects that have type names. Variables are declared with enum specifiers in the place of the type specifier. An enumerated type can have one of the following forms: enum { enumerator,... } enum tag { enumerator,... } enum tag Each enumerator defines a constant of the enumerated type (tag). The enumerator list forms an ordered list of the type's values. Each enumerator has the form "identifier [= expression]", where the "identifier" is the name to be used for the constant value and the optional "expression" gives its integer equivalent. If a tag appears but no list of enumerators, the enum-specifier refers to a previous definition of the enumerated type, identified by the tag. The following example declares an enumerated object 'background_color' with a list of enumeration constants: enum colors { black, red, blue, green, } background_color; Later in the program, a value can be assigned to the object 'background_color': background_color = red; In this example, the compiler automatically assigns the integer values as follows: black = 0, red = 1, blue = 2, and green = 3. Alternatively, explicit values can be assigned during the enumerated type definition: enum colors { black = 5, red = 10, blue, green = black+2 }; Here, black equals the integer value 5, red = 10, blue = 11, and green = 7. Note that blue equals the value of the previous constant (red) plus one, and green is allowed to be out of sequential order. Because the ANSI C standard is not strict about assignment to enumerated types, any assigned value not in the predefined list is accepted without complaint.

Pointer

A pointer in C is a variable that holds the address of another variable. Pointers are declared with the asterisk operator. For example: int i, *ip, *np; /* i IS AN INTEGER, ip AND np ARE POINTERS TO INTEGERS */ The following operations are permitted on pointers: o Assigning an address to the pointer (as in ip = &i;) o Fetching the object of the pointer (by dereferencing the pointer) with the asterisk operator (i = *ip;, which assigns the addressed integer to i) o Adding (as in ip += 5;, which makes ip point to the object that is five longwords away from the initial address in ip) o Subtracting (as in i = np - ip;, which gives the number of objects separating the objects pointed to by np and ip)

Structure

A structure is an aggregate of members whose data types can differ. Members can be scalar variables, arrays, structures, unions, and pointers to any object. The size of a structure is the sum of the sizes of its members, which are stored in the order of their declarations. Structures are defined with the keyword struct, followed by an optional tag, followed by a structure-declaration list in braces. The syntax is: struct [identifier] { struct-declaration ... } Each struct-declaration is a type specifier (type keyword, struct tag, union tag, enum tag, or typedef name) followed by a list of member declarators: type-specifier member-declarator,... ; Each member declarator defines either an ordinary variable or a bit field: declarator or [declarator] : constant-expression The following example declares a new structure employee with two structure variables bob and susan of the structure type employee: struct employee { char *name; int birthdate; /* name, birthdate, job_code, and salary are */ int job_code; /* treated as though declared with const. */ float salary; }; struct employee bob, susan;

typedef

Use the typedef keyword to define an abbreviated name, or synonym, for a lengthy type definition. Grammatically, typedef is a storage-class specifier, so it can precede any valid declaration. In such a declaration, the identifiers name types instead of variables. For example: typedef char CH, *CP, STRING[10], CF(); In the scope of this declaration, CH is a synonym for "character," CP for "pointer to character," STRING for "10-element array of characters," and CF for "function returning a character." Each of the type definitions can be used in that scope to declare variables. For example: CF c; /* c IS A FUNCTION RETURNING A CHARACTER */ STRING s; /* s IS A 10-CHARACTER STRING */

Union

A union is an aggregate of members whose data types can differ. Members can be scalar variables, arrays, structures, unions, and pointers to any object. The size of a union is the size of its longest member plus any padding needed to meet alignment requirements. All its members occupy the same storage. Unions are defined with the union keyword, followed by an optional tag, followed by a union-declaration list in braces. The syntax is: union [identifier] { union-declaration ... } Each union-declaration is a type specifier (type keyword, struct tag, union tag, enum tag, or typedef name) followed by a list of member declarators: type-specifier member-declarator,... ; Each member declarator defines either an ordinary variable or a bit field: declarator or [declarator] : constant-expression Once a union is defined, a value can be assigned to any of the objects declared in the union declaration. For example: union name { dvalue; struct x { int value1; int value2; }; float fvalue; } alberta; alberta.dvalue = 3.141596; /*Assigns pi to the union object*/ Here, alberta can hold a double, struct, or float value. The programmer has responsibility for tracking the current type contained in the union. The type is maintained until explicitly changed. An assignment expression can be used to change the type of value held in the union.

Void

You can use the void data type to declare functions that do not return a value. Functions declared to be of this type cannot contain return statements and cannot be used in statements where a return value is expected. The void data type can be used in the cast operation if casting to a "function without a return value ...". You can also use the void data type with pointers.

Privacy statement Using this site means you accept its terms
© 2007 Hewlett-Packard Development Company, L.P.