6.1.1 dict_apply This function has the (nontrivial) type: void dict_apply (const dict_t* dict, const dict_apply_fun_t fun, void* arg); For each pair key/value in dict, it will call the function fun with arguments (key, value, arg). This is a type of "for each" loop (or map), showing that C is well capable of using advanced programming concepts, albeit with a somewhat contrived syntax. The type dict_apply_fun_t is the interesting part here: typedef void (*dict_apply_fun_t) (const char* key, const char* val, void* arg); This defines a type dict_apply_fun_t as "pointer to a function that returns void and takes two arguments of type const char* and one of type void* (untyped pointer)". Indeed, functions have addresses (more on that in Ch. 7), so one can talk about pointers to functions. To call the function pointed at by fun, one can use a normal function-call syntax (fun (key, value, arg)) or dereference the pointer first ((*fun) (key, value, arg))-this is a matter of taste. In the following example (print.c), we create a dictionary, put a few things in it, then call dict_apply to print all the values in the dictionary. We use the extra argument arg to count the number of elements. We need to retype arg since dict_apply demoted its type to a generic pointer void*. #include #include "dict.h" void print_pair (const char* key, const char* val, void* arg) { } // Retype arg int* pi = arg; printf("%d. key: %s, val: %s\n", *pi, key, val); ("pi)++; Activate Windows Go to Settings to activate Windows. int main (8

icon
Related questions
Question
6.1.1 dict_apply
This function has the (nontrivial) type:
void dict_apply (const dict_t* dict, const dict_apply_fun_t fun, void* arg);
For each pair key/value in dict, it will call the function fun with arguments (key, value, arg). This is a type of "for each" loop (or map), showing that C is well capable of using
advanced programming concepts, albeit with a somewhat contrived syntax. The type dict_apply_fun_t is the interesting part here:
typedef void (*dict_apply_fun_t) (const char* key, const char* val, void* arg);
This defines a type dict_apply_fun_t as "pointer to a function that returns void and takes two arguments of type const char* and one of type void* (untyped pointer)". Indeed, functions
have addresses (more on that in Ch. 7), so one can talk about pointers to functions. To call the function pointed at by fun, one can use a normal function-call syntax (fun (key, value,
arg)) or dereference the pointer first ((*fun) (key, value, arg))-this is a matter of taste.
In the following example (print.c), we create a dictionary, put a few things in it, then call dict_apply to print all the values in the dictionary. We use the extra argument arg to count the
number of elements. We need to retype arg since dict_apply demoted its type to a generic pointer void*.
#include <stdio.h>
#include "dict.h"
void print_pair (const char* key, const char* val, void* arg) {
}
// Retype arg
int* pi = arg;
printf("%d. key: %s, val: %s\n", *pi, key, val);
("pi)++;
Activate Windows
Go to Settings to activate Windows.
int main (8
Transcribed Image Text:6.1.1 dict_apply This function has the (nontrivial) type: void dict_apply (const dict_t* dict, const dict_apply_fun_t fun, void* arg); For each pair key/value in dict, it will call the function fun with arguments (key, value, arg). This is a type of "for each" loop (or map), showing that C is well capable of using advanced programming concepts, albeit with a somewhat contrived syntax. The type dict_apply_fun_t is the interesting part here: typedef void (*dict_apply_fun_t) (const char* key, const char* val, void* arg); This defines a type dict_apply_fun_t as "pointer to a function that returns void and takes two arguments of type const char* and one of type void* (untyped pointer)". Indeed, functions have addresses (more on that in Ch. 7), so one can talk about pointers to functions. To call the function pointed at by fun, one can use a normal function-call syntax (fun (key, value, arg)) or dereference the pointer first ((*fun) (key, value, arg))-this is a matter of taste. In the following example (print.c), we create a dictionary, put a few things in it, then call dict_apply to print all the values in the dictionary. We use the extra argument arg to count the number of elements. We need to retype arg since dict_apply demoted its type to a generic pointer void*. #include <stdio.h> #include "dict.h" void print_pair (const char* key, const char* val, void* arg) { } // Retype arg int* pi = arg; printf("%d. key: %s, val: %s\n", *pi, key, val); ("pi)++; Activate Windows Go to Settings to activate Windows. int main (8
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer