So that's what I'm doing. I have my book. I work through exercises. I read the PostGIS code. It's a slow process, but rewarding as my understanding grows.
For those of you who, like me, have mostly worked in higher level languages, I want to share my C "wow" moment for the week. C has arrays. The syntax is the same as (surprise) all those other languages (Java, Perl, Javascript, PHP) that ape C syntax. Want to iterate through an array? No problem, very familiar, we print out the contents of our array:
for( i = 0; i < sizeof(array); i++ ) {
printf( "%d\n", array[i] );
}
Now, I knew C pointers were much less abstract than Java pointers, they actually point to memory addresses. Even so, there's knowing and then there is KNOWING. This routine, that also prints the contents of the array, blew my mind:
for( i = 0; i < sizeof(array); i++ ) {
printf( "%d\n", *(array + i) );
}
WTF!?!
First, it turns out that the value of the bare "array" variable is just a pointer to the front of the array (how efficient). But the icing on the cake is that you can do math on the pointers! I add 1 to the pointer, and now it's pointing at the next element, so when I dereference the pointer (with that *) out pops the next value!
All you CompSci majors can have a laugh at my expense ("technopeasant!"), but I'm self-taught, and I have been living in other people's (Perl, Java, PHP, Avenue (!!!), Javascript) interpreters for many years. This stuff is too cool.

7 comments:
Beware, you're playing with funny dynamite. The moment you try to do your pointer math out of the actually allocated array only higher powers know what will happen (nothing good in any case) ;)
As an added WTF moment, the array indexing syntax array[i] is just shorthand for *(array+i), as you noted. Since addition commutes, the following indexing also works: i[array]. Using this syntax however is a quick way to annoy your fellow developers.
Yeah, so this brings back some memories of my comp sci days... but your complete wonderment and awe demonstrates the more exciting way to really learn something is through discovery.
Have fun.
Welcome to the Real Programmer's club!
It's nice that you're finding this cool... I suspect that after a few index-past-end-of-array and segfault issue, you may be proclaiming this "feature" more of an FPOS.
At which point I look forward to welcoming you to the "Cool Programmers" club living in the JVM! 8^)
Glad you are enjoying your new life. Ah all that C stuff now that you mention it is beginning to come back to me now. Hmm maybe its not as night-marish as I remembered.
You should add C# to your list of C imposters.
I might give it a try again sometime too.
Do you really want to go down this path the week before the NHL playoffs begin?
there's nothing wrong with Avenue (!!!)
Post a Comment