9 comments

[ 4.5 ms ] story [ 46.6 ms ] thread
Nitpicky but:

    The remaining arguments &haystack, &haystack_len, &needle, &offset specify the variables to put the arguments into. As you can see, they are all passed by reference (&), which means that not the variables themselves are passed, but pointers to them.

References aren't pointers, they are the variables. The address of the variable itself gets passed with a reference, whereas a pointer is a variable containing the address of the variable. A pointer itself has an address. A very subtle yet important difference.
A very important difference for a C developer. For a PHP developer reading C, it's a pretty good analogy that gets the point across without causing too much confusion...
A pointer doesn't necessarily have an address - think of the semantics of the register storage class ;) Or perhaps more commonly in C, when a pointer to a variable is passed by value as an argument to a function in a register.

Of course, trying to take the address of a variable declared to be in the register storage class is an error, and taking the address of a variable in the normal automatic storage class typically forces it to live on the stack.

Pointers have an address, in the sense that it is semantically correct to take their address. If they happen to reside in a register at a given moment, that is an optimization and doesn't change their semantics.
Another error:

      At this point you’ll have to remember that in C, arrays are represented by pointers to their first value.
No, they aren't. Arrays can _decay_ to a pointer when passed as function parameters, but arrays and pointers are different things. Between these two items, I think the author either a) is talking down to his audience more than is warranted or b) fundamentally misunderstands C.
To whomever downvoted this: if you think I'm wrong, I suggest you look at question 6.3 from the comp.lang.c faq: http://c-faq.com/aryptr/aryptrequiv.html

If you think I'm being too pedantic, a reply rather than a mere downvote would be appreciated.

I didn't downvote, but I do think it's a bit pedantic.

From my dogeared, beaten copy of K&R: "By definition, the value of a variable or expression of type array is the address of element zero of the array."

I don't think what the author wrote expresses that array === pointer, they simply made a statement about the underlying representation.

The truth lies somewhere between a) and b).

I don't really know C well, as I'm mainly programming in PHP. Sure, I can read and write it, but I definitely don't know the details of the language (and yes, I'd consider that a detail as the only difference I know of is the behavior of sizeof).

There's more to it than just sizeof, for example an array of arrays is structured completely differently than an array of pointers to other arrays, even if you can still access items via a[i][j].