Programming in C

(Barry) #1
4.0 Data Types and Declarations 437

Va r iables can be declared at the time that the union is defined, or they can be subse-
quently declared using the notation
union name variableList;
provided the union was given a name when it was defined.
It is the programmer’s responsibility to ensure that the value retrieved from a union is
consistent with the last value that was stored inside the union.The firstmember of a
union can be initialized by enclosing the initial value, which, in the case of a global
union variable, must be a constant expression, inside a pair of curly braces:
union shared
{
long long int l;
long int w[2];
} swap = { 0xffffffff };
This declares the union variable swapand sets the lmember to hexadecimal ffffffff.
A different member can be initialized instead by specifying the member name, as in
union shared swap2 = {.w[0] = 0x0, .w[1] = 0xffffffff; }
An automatic union variable can also be initialized to a union of the same type, as in
union shared swap2 = swap;

4.3.4 Pointers
The basic format for declaring a pointer variable is as follows:
type *name;
The identifier nameis declared to be of type “pointer to type,” which can be a basic data
type, or a derived data type. For example,
int *ip;
declares ipto be a pointer to an int, and the declaration
struct entry *ep;
declares epto be a pointer to an entrystructure.
Pointers that point to elements in an array are declared to point to the type of ele-
ment contained in the array. For example, the previous declaration of ipcan also be used
to declare a pointer into an array of integers.
More advanced forms of pointer declarations are also permitted. For example, the
declaration
char *tp[100];
declares tpto be an array of 100 character pointers, and the declaration
struct entry (*fnPtr) (int);

20 0672326663 AppA 6/10/04 2:01 PM Page 437

Free download pdf