Nick Desaulniers

The enemy's gate is down

Jan 26, 2013 - 3 minute read - Comments - C function pointer syntax

C Function Pointers Alternate Syntax

On an interview with Square, I made the mistake of stating that one of the benefits of working with JavaScript over C is that functions are first class in JavaScript, therefore they may be passed around. To which the interviewer replied, “Well, C can do that, what about function pointers?” What? Luckily, I was able to get out of that jam by guessing that JavaScript had a nicer syntax.

While I was taught some C in university, we had never gone over function pointers or more in depth topics such as static or dynamic linkage. I was so embarrassed that my expensive (read overpriced) degree had not taught me more about the C programming language, especially from the Computer Engineering department that focuses on software AND hardware. On my exit interview with the dean, I was very opinionated on the amount of C that was (or wasn’t) taught at my university. His argument was that there are only so much of so many languages you can cover in a university, which to some extent is valid. My problem has been that I’ve enjoyed learning many different programming languages, though I didn’t really get it the first time around (with Java). I think knowing many different languages and their respective paradigms makes you a better programmer in other languages, since you can bring a Ruby Way to Rust or a JavaScript functional style into C.

I had read two books in the meantime that really flushed out more of the C language to me and I would definitely recommend them to those who want to learn more about the language. They are Head First C by Dave and Dawn Griffiths and 21st Century C by Ben Klemens. I’m also aware that The C Programming Language by Brian Kernighan and Dennis Ritchie is also known as the canonical text. The book is often referred to as ‘K&R’ after the authors’ initials, and Dennis Ritchie was the original creators of the C language and co-developer of Unix. I’d love to get a chance to read it someday.

This article appeared on Hacker News and really piqued my interest. Defenitely a great read. What really stuck out to me was one of the comments though. The author of the comment mentioned a less ugly syntax for function pointers, with a link to an example. Now I’m not sure what the commenter meant by “these params decay naturally to function pointers” but I was skeptical about this different syntax. Even the Wikipedia article used the syntax that I was familiar with. So I wrote up a quick example to try it:

// compiled with:
// clang -Wall -Wextra function_pointers.c -o function_pointers
#include "stdio.h"

void usual_syntax (void (*fn) (int x)) {
  puts("usual syntax start");
  fn(1);
  puts("usual syntax end");
}

void other_syntax (void fn (int x)) {
  puts("other syntax start");
  fn(2);
  puts("other syntax end");
}

void hello (int x) {
  printf("hello %d\n", x);
}

int main () {
  puts("hello world");
  usual_syntax(hello);
  other_syntax(hello);
}

Sure enough we get:

hello world
usual syntax start
hello 1
usual syntax end
other syntax start
hello 2
other syntax end

So the moral to the story I guess is that there’s always more to your favorite language. From using variadic macros with compound literals to enable a more functional style in C to reflecting upon a function’s number of arguments or name or adding attributes to a function or the y-combinator in JavaScript, I learn something new every day. And I hope that you did too! Thanks for reading! If you have some other recommendations on good programming books, or design patterns, please leave a comment or write a reply blog post!

[Internet] Freedom Is Not Free Commandments of a Mobile Web

comments powered by Disqus