Sunday, May 15, 2011

Function pointers in C

This time I'll tackle a C language construct. While C may be a very shallow abstraction of a computational machine, it still has a lot of punch to offer.

Functions are sections of code that can be invoked. Sometimes we do know what the function looks like before we invoke it, but we don't have a definition for it at compile time (a function imported from a shared dynamic library for example). Function pointers fit for this use case perfectly.

In the following code snippet I show a very simple example of using function pointers.

#include "stdlib.h"

typedef int (*intfun) ();

int u(){
return 1;

int v(){
return 2;

int main(){
intfun f = &u;
printf("Hello there %i\n", (*f)());
printf("Hello there %i\n", f());
f = v;
printf("Hello there %i\n", (*f)());
printf("Hello there %i\n", f());
return 0;

The typedef defines that `intfun` is a type, such that if any `a` was of type `intfun`, then dereferencing `a` and calling it would be the same as calling a function that takes no parameters and returns `int`.

In the code snippet above (which I so far tried compiling in MinGW on Windows), `f` is declared to be a variable of type `intptr` and it is initialized by passing it the address of function `u`. To call `u` through `f`, you can dereference `f` and then pass it an empty parameter list. However, the C compiler is smart enough to dereference the function pointer implicitly if it was invoked with a parameter list.
We can also see this magic happening when assigning values to `f`. We don't need to use the address of operator with `v` to assign it to `f`.

In future posts I'll show how it is possible to use function pointers to get rid of annoying large switch block statements that scream "procedural".

Monday, May 9, 2011

Javascript Explorations

Transfering experience from Actionscript to Javascript entails many levels of mind mappings. On the practical level, Actionscript allows the developer a great deal of flexibility and expressibility within a concise object oriented framework.

One of the tomes of power that Actionscript keeps the throne at is animation.
While in the realm of Actionscript we'd be adding event listeners to `ENTER_FRAME`, in Javascript we'd be passing a callback to the `setInterval` function. Enough about Actionscript, let's get down to Javascript.

Any kind of architectural enforcing could work to make things more sane in Javascript land (we don't want to be setting out setInterval sporadocally throughout our code). The downside is that each developer, or each `plug-your-code-to-me` framework may want to do it in a slightly different way, and it's practically impossible to get two frameworks to play nice together if they were all-encompassing in that regard.
This develops a great desire for exploring the method of orchestrating all these animations that jQuery UI Animations take route. Do they have a single `setInterval` that they turn on and off based on what they need? Or do they just spawn out a setInterval for each different thing going on a web page? If it were a single invocation, then is it expected to be exclusive for UI updates, in case we're allowed to hoop up on it in the first place?

Let's hope these questions will lead to some concise and definite answers.

All this talk about animation got my itch going to develop this bit of Javascript test:

var list = [];
var x = document.getElementById('x');

setInterval(function() {
var box = {
d: document.createElement('div'),
t: 0,
x0: Math.random()*400,
y0: Math.random()*400,

box.x = box.x0;
box.y = box.y0;

var d = box.d; = '5px'; = '5px'; = "#000"; = 'relative'; = '100px'; = '200px';

}, 500);

setInterval(animateBoxes, 20);

function animateBoxes(){
for(var i = 0; i < list.length; i++){
var b = list[i];
b.x = b.x0 + b.t*10;
b.y = b.y0 + Math.sin(2*b.t)*30; = b.x + "px"; = b.y + "px";

b.t += 0.02;

I got it tested here too if you don't really want to create your own files for the testing: