20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

450 Appendix A C Language Summary<br />

a + n<br />

*(a + n)<br />

produces a po<strong>in</strong>ter to element number n of a, has type “po<strong>in</strong>ter<br />

to t,” and is <strong>in</strong> all ways equivalent to the expression &a[n];<br />

references element number n of a, has type t, and is <strong>in</strong> all ways<br />

equivalent to the expression a[n].<br />

The actual type of the <strong>in</strong>teger produced by subtract<strong>in</strong>g two po<strong>in</strong>ters is specified by<br />

ptrdiff_t, which is def<strong>in</strong>ed <strong>in</strong> the standard header file .<br />

Po<strong>in</strong>ters to Structures 2<br />

Given that<br />

x is an lvalue expression of type struct s;<br />

ps<br />

is a modifiable lvalue expression of type “po<strong>in</strong>ter to struct s”;<br />

m is the name of a member of the structure s and is of type t;<br />

v<br />

is an expression;<br />

then the expression<br />

&x<br />

produces a po<strong>in</strong>ter to x and is of type “po<strong>in</strong>ter to struct s”;<br />

ps = &x<br />

ps->m<br />

(*ps).m<br />

ps->m = v<br />

sets ps po<strong>in</strong>t<strong>in</strong>g to x and is of type “po<strong>in</strong>ter to struct s”;<br />

references member m of the structure po<strong>in</strong>ted to by ps and is of<br />

type t;<br />

also references this member and is <strong>in</strong> all ways equivalent to the expression<br />

ps->m;<br />

stores the value of v <strong>in</strong>to the member m of the structure po<strong>in</strong>ted to by<br />

ps and is of type t.<br />

5.16 Compound Literals<br />

A compound literal is a type name enclosed <strong>in</strong> parentheses followed by an <strong>in</strong>itialization<br />

list. It creates an unnamed value of the specified type, which has scope limited to the<br />

block <strong>in</strong> which it is created, or global scope if def<strong>in</strong>ed outside of any block. In the latter<br />

case, the <strong>in</strong>itializers must all be constant expressions.<br />

As an example,<br />

(struct po<strong>in</strong>t) {.x = 0, .y = 0}<br />

is an expression that produces a structure of type struct po<strong>in</strong>t with the specified <strong>in</strong>itial<br />

values.This can be assigned to another struct po<strong>in</strong>t structure, as <strong>in</strong><br />

orig<strong>in</strong> = (struct po<strong>in</strong>t) {.x = 0, .y = 0};<br />

or passed to a function expect<strong>in</strong>g an argument of struct po<strong>in</strong>t, as <strong>in</strong><br />

moveToPo<strong>in</strong>t ((struct po<strong>in</strong>t) {.x = 0, .y = 0});<br />

2. Also applies to unions.

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

Saved successfully!

Ooh no, something went wrong!