2

I'm following Learn C The Hard Way online.

At a point the author talks about "dynamic callback" to a function.

Could someone explain me what it is exactly because I didn't get it?

EDIT

For context, here's an extract from Learn C the Hard Way:

Practical Pointer Usage

There are primarily four useful things you can do with pointers in C code:

• Ask the OS for a chunk of memory and use a pointer to work with it. This includes strings and something you haven’t seen yet, structs.

• Pass large blocks of memory (like large structs) to functions with a pointer, so you don’t have to pass the whole thing to them.

• Take the address of a function so you can use it as a dynamic callback.

• Scan complex chunks of memory, converting bytes off of a network socket into data structures or parsing files.

For nearly everything else, you might see people use pointers when they should be using arrays. In the early days of C programming, people used pointers to speed up their programs, because the compilers were really bad at optimizing array usage. These days, the syntax to access an array versus a pointer are translated into the same machine code and optimized in the same way, so it’s not as necessary. Instead, you should go with arrays whenever you can, and then only use pointers as a performance optimization if you absolutely have to.

Excerpt From: Zed A. Shaw. “Learn C the Hard Way: Practical Exercises on the Computational Subjects You Keep Avoiding (Like C) (Brianne Kwasny's Library).”

wide_eyed_pupil
  • 3,153
  • 7
  • 24
  • 35
sloan
  • 319
  • 1
  • 3
  • 9
  • 2
    That could mean several things. Could you include more than two words from the author? – Paul Draper Dec 03 '13 at 18:12
  • Probably just a callback. func x(function_pointer); you ten create function for your callback and pass it to x which will call at some time. – Jan Matějka Dec 03 '13 at 18:15
  • @PaulDraper the phrase "dynamic callback" is mentioned exactly twice in LCTHW: http://c.learncodethehardway.org/book/ex15.html and http://c.learncodethehardway.org/book/ex47.html. – Jacob Krall Dec 03 '13 at 18:17
  • @PaulDraper yes, OP asked about "C the hard way" – Jacob Krall Dec 03 '13 at 18:22

1 Answers1

2

One thing the author could be talking about is a function pointer

function getTokens(void (*callback)(char *)) {
   char *c;
   while(true) {
       c = malloc(100 * sizeof(char));
       if(scanf("%s", c) != 1) {
           break;
       }
       callback(c);
   }
   calloc(c);
}

Here, the function callback is called with each string read from standard input.

callback could do anything, and it passed to getTokens dynamically at runtime.

void printToken(char *c data) {
    printf("%s\n", data);
}

void ignoreToken(char *c) {
}

void (*tokenCallback)(char *) = (1 + 1 == 2) ? &printToken : &ignoreToken; //"dynamic"
getTokens(tokenCallback);
Paul Draper
  • 78,542
  • 46
  • 206
  • 285
  • Great answer. It might be nice to show how this looks from the calling side - e.g. calling `getTokens` with two different callbacks. – Jacob Krall Dec 03 '13 at 18:25