Programming in C

(Barry) #1
450 Appendix A C Language Summary

a + n produces a pointer to element number nof a, has type “pointer
to t,” and is in all ways equivalent to the expression &a[n];
*(a + n) references element number nof a, has type t, and is in all ways
equivalent to the expression a[n].
The actual type of the integer produced by subtracting two pointers is specified by
ptrdiff_t, which is defined in the standard header file <stddef.h>.

Pointers to Structures^2
Given that
x is an lvalueexpression of type struct s;
ps is a modifiable lvalue expression of type “pointer to struct s”;
m is the name of a member of the structure sand is of type t;
v is an expression;
then the expression
&x produces a pointer to xand is of type “pointer to struct s”;
ps = &x sets pspointing to xand is of type “pointer to struct s”;
ps->m references member mof the structure pointed to by psand is of
type t;
(*ps).m also references this member and is in all ways equivalent to the expres-
sion ps->m;
ps->m = v stores the value of vinto the member mof the structure pointed to by
psand is of type t.

5.16 Compound Literals


A compound literal is a type name enclosed in parentheses followed by an initialization
list. It creates an unnamed value of the specified type, which has scope limited to the
block in which it is created, or global scope if defined outside of any block. In the latter
case, the initializers must all be constant expressions.
As an example,
(struct point) {.x = 0, .y = 0}
is an expression that produces a structure of type struct pointwith the specified initial
values.This can be assigned to another struct pointstructure, as in
origin = (struct point) {.x = 0, .y = 0};
or passed to a function expecting an argument of struct point, as in
moveToPoint ((struct point) {.x = 0, .y = 0});


  1. Also applies to unions.


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

Free download pdf