20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

4.0 Data Types and Declarations<br />

437<br />

Variables can be declared at the time that the union is def<strong>in</strong>ed, or they can be subsequently<br />

declared us<strong>in</strong>g the notation<br />

union name variableList;<br />

provided the union was given a name when it was def<strong>in</strong>ed.<br />

It is the programmer’s responsibility to ensure that the value retrieved from a union is<br />

consistent with the last value that was stored <strong>in</strong>side the union.The first member of a<br />

union can be <strong>in</strong>itialized by enclos<strong>in</strong>g the <strong>in</strong>itial value, which, <strong>in</strong> the case of a global<br />

union variable, must be a constant expression, <strong>in</strong>side a pair of curly braces:<br />

union shared<br />

{<br />

long long <strong>in</strong>t l;<br />

long <strong>in</strong>t w[2];<br />

} swap = { 0xffffffff };<br />

This declares the union variable swap and sets the l member to hexadecimal ffffffff.<br />

A different member can be <strong>in</strong>itialized <strong>in</strong>stead by specify<strong>in</strong>g the member name, as <strong>in</strong><br />

union shared swap2 = {.w[0] = 0x0, .w[1] = 0xffffffff; }<br />

An automatic union variable can also be <strong>in</strong>itialized to a union of the same type, as <strong>in</strong><br />

union shared swap2 = swap;<br />

4.3.4 Po<strong>in</strong>ters<br />

The basic format for declar<strong>in</strong>g a po<strong>in</strong>ter variable is as follows:<br />

type *name;<br />

The identifier name is declared to be of type “po<strong>in</strong>ter to type,” which can be a basic data<br />

type, or a derived data type. For example,<br />

<strong>in</strong>t *ip;<br />

declares ip to be a po<strong>in</strong>ter to an <strong>in</strong>t, and the declaration<br />

struct entry *ep;<br />

declares ep to be a po<strong>in</strong>ter to an entry structure.<br />

Po<strong>in</strong>ters that po<strong>in</strong>t to elements <strong>in</strong> an array are declared to po<strong>in</strong>t to the type of element<br />

conta<strong>in</strong>ed <strong>in</strong> the array. For example, the previous declaration of ip can also be used<br />

to declare a po<strong>in</strong>ter <strong>in</strong>to an array of <strong>in</strong>tegers.<br />

More advanced forms of po<strong>in</strong>ter declarations are also permitted. For example, the<br />

declaration<br />

char *tp[100];<br />

declares tp to be an array of 100 character po<strong>in</strong>ters, and the declaration<br />

struct entry (*fnPtr) (<strong>in</strong>t);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!