On 10/14/2011 8:56 AM, Toby Thain wrote:
On 14/10/11 2:13 AM, Pontus Pihlgren wrote:
On Thu, Oct 13, 2011 at 09:14:34PM +0100, Tony
Duell wrote:
for(int
i=0;i<14;i++)
putchar(i["hello world\n"]);
Not very complex, but figuring out how it works i fun :-)
What's to 'work out'? The argument to putchar looks a bit odd, but IIRC,
A[i] is a shorthand for *(A+i), and addition is commutitive (even
when it
involves pointers). So that's just indexing an array of chracters (the
string constant) using i.
Hey Tony, cut me some slack. Not all C programmers will recall(or even
know to begin with) that A[i] is shorthand for *(A+1). I learned it
through this example.
Here is another one which also requires some knowledge of standards:
The example *above* doesn't require reading any standards; it's basic C
usage and is in K&R 1. It would be hard to read or write C in any
serious way without having internalised things like this. (Because both
constructs are common.)
Pontus,
For what it's worth, I didn't know this "rule" prior to your example.
And I've been programming C for myself for 20+ years. I can read and
write C enough to accomplish my goals. When I run into a construct I
haven't seen before, a little research, and I understand it enough to
use it.
In general, I write the cleanest simplest code that will get the job
done. Concise code is good, but if there's a conflict, clear code is better.
If I use anything that might be a bit tricky (I know I'm just a mere
mortal, but Pontus' stuff counts) then I make sure that there's good
enough comments to explain what I'm doing. This helps me and helps
anyone else that might be reading the code. When I start writing these
types of comments, however, I often just delete the block of code, and
do it in a simpler way. Simpler doesn't necessary mean less efficient.
And of course all my code is commented.
Keith