Expert C Programming

(Jeff_L) #1

char * ; step 5 say "pointer to..."


char ; step 6 say "char;"


Then put it all together to read:


"c is an array[0..9] of pointer to a function returning a pointer-to-char"


and we're done. Note: the fuctions pointed to in the array take a pointer to a pointer as their
one and only parameter.


Chapter 4. The Shocking Truth: C Arrays and Pointers


Are NOT the Same!


Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper
consideration.


—Stan Kelly-Bootle


arrays are NOT pointers...why doesn't my code work?...what's a declaration? what's a
definition?...match your declarations to the definition...array and pointer differences...some light
relief—fun with palindromes!


Arrays Are NOT Pointers!


One of the first things that novice C programmers often hear is that "arrays are the same as pointers."
Unfortunately, this is a dangerous half-truth. The ANSI C Standard paragraph 6.5.4.2 recommends
that you


Note the distinction between the declarations:


extern int *x;


extern int y[];


The first declares x to be a pointer to int; the second declares y to be an array of int of


unspecified size (an incomplete type), the storage for which is defined elsewhere.


The standard doesn't go into the matter in any greater detail than that. Too many C books gloss over
when arrays are, and are not, equivalent to pointers, relegating the explanation to a footnote when it
should be a chapter heading. This book tries to restore the balance by fully explaining when arrays are
equivalent to pointers, when they are not, and why. Not only that, but we also make sure that the key
point is emphasized with a chapter heading, not a footnote.


Why Doesn't My Code Work?


If I had a dime for every time someone brought me a program like the following, together with the
complaint that "it doesn't work," then I'd have, uh, let's see, about two-fifty.

Free download pdf