Thursday, April 03, 2008

See?

One of the things I wanted to do after moving on from Refractions was get back into technology in a "hands on" way again, and the place I most want to get my hands dirty is with PostGIS. It's all very nice to be a technology evangelist, but very frustrating to have to depend entirely on others to get things implemented. I have to be my own staff now, and that means if I want to play with the guts of PostGIS, I have to learn C.

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:

Andrea Aime said...

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) ;)

Andy said...

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.

Krista said...

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.

Dr JTS said...

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^)

Regina Obe said...

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.

BrianT said...

Do you really want to go down this path the week before the NHL playoffs begin?

el samo said...

there's nothing wrong with Avenue (!!!)

About Me

My Photo
Victoria, British Columbia, Canada

Followers

Blog Archive

Labels

bc (35) it (27) postgis (19) icm (11) enterprise IT (10) video (10) sprint (9) open source (8) osgeo (8) cio (6) management (6) enterprise (5) foippa (5) foss4g (5) gis (5) spatial it (5) foi (4) mapserver (4) outsourcing (4) politics (4) bcesis (3) oracle (3) COTS (2) architecture (2) boundless (2) esri (2) idm (2) natural resources (2) ogc (2) open data (2) opengeo (2) openstudent (2) postgresql (2) rant (2) technology (2) vendor (2) web (2) 1.4.0 (1) HR (1) access to information (1) accounting (1) agile (1) aspen (1) benchmark (1) buffer (1) build vs buy (1) business (1) business process (1) cathedral (1) cloud (1) code (1) common sense (1) consulting (1) contracting (1) core review (1) crm (1) custom (1) data warehouse (1) deloitte (1) design (1) digital (1) email (1) essentials (1) evil (1) exadata (1) fcuk (1) fgdb (1) fme (1) foocamp (1) foss4g2007 (1) ftp (1) gds (1) geocortex (1) geometry (1) geoserver (1) google (1) google earth (1) government (1) grass (1) hp (1) iaas (1) icio (1) industry (1) innovation (1) integrated case management (1) introversion (1) iso (1) isss (1) isvalid (1) javascript (1) jts (1) lawyers (1) mapping (1) mcfd (1) microsoft (1) mysql (1) new it (1) nosql (1) opengis (1) openlayers (1) oss (1) paas (1) pirates (1) policy (1) portal (1) proprietary software (1) qgis (1) rdbms (1) recursion (1) redistribution (1) regression (1) rfc (1) right to information (1) saas (1) salesforce (1) sardonic (1) seibel (1) sermon (1) siebel (1) snark (1) spatial (1) standards (1) svr (1) tempest (1) texas (1) tired (1) transit (1) twitter (1) udig (1) uk (1) uk gds (1) verbal culture (1) victoria (1) waterfall (1) wfs (1) where (1) with recursive (1) wkb (1)