Getting Started

(lily) #1

Chapter 8: C Pointers and Arrays


ointer to
eturning char.’ Yes, they are serious, and I have
h worse. Be very afraid.

is a pointer to an int

ptrFred = &z[0]; //iptrFred now points to the first element in array z

ce or absence of the indirection operator as it
tion’ and
‘dereference’ as the names of the operator, when one weird word would have been


ds 10 to the content of z[0]

e content of z[0]
Fred)++; // after using the content of z[0] increment it to z[1]
trFred++; // iptrFred now points to z[1]

char ((x())[])()


which is on p. 126 of K&R and translates to: ‘x is a function returning a p
an array of poin
ncountered muc


ters to functions r
e


Let’s look at some examples:


int x = 10, y = 20, z[30];
int * iptrFred; // iptrFred


iptrFred = &x; //iptrFred now contains the address of the variable x
y = iptrFred; // y is now equal 10;
iptrFred = 0; // x is now equal 0
i


Pay careful attention to the presen
dereferences and wonder why on earth they chose both ‘indirec


plenty?


More examples:


iptrFred = iptrFred + 10; // ad
iptrFred += 10; // same as above
y =
ptrFred + 20; // sets y equal to the content of z[0] + 20
++iptrFred; // increments th
(
iptr
*ip


The last two may have caused you to go ‘say what?’ It has to do with operator
precedence. Now is a good time to thank the stars that this is a self-teaching book
and I can’t test you on this stuff.


Function Arguments


Arguments are passed to functions by value, and as we saw in an earlier
discussion of functions, there is no way for a function to affect the value the
variable passed to it in the function that did the passing. Okay, Let’s show an
example:

Free download pdf