About before class

Most of the time, reading articles is not to learn something in depth, in fact, most of the time, I want to see what others are doing, what is new, what can be used for reference, in order to expand my horizon. Personally, I’m just lazy and don’t want to mess with anything. I just want to see what others do, just as a spectator. One day, I suddenly saw something that someone was doing and was so interested that I wanted to see all the things he was doing. So this place is like a pit where I play with C and C, and I don’t know how long it will last. As for C, I belong to the beginner and make progress together.

I recommend bellard, a French god, who is really not familiar with audio and video encoding and decoding, and compilation principle. It is not too much to say that he is a genius. He can shoulder to shoulder with Linus Torvalds, and his achievements on Zhihu are introduced.

Callback

Functions in C are also Pointers, called function Pointers. You can use a typedef void (*callback_t)(char *) to define a new type called callback_t, which is a function pointer.

First question: Why is there an asterisk before callback_t? That means it’s a pointer type.

Second question: is a pointer can be seen, but why is a function pointer? It’s because of the second pair of parentheses behind it, and without the second pair of parentheses behind it it’s just a pointer.

Here’s the complete code:

#include <stdio.h>

typedef void (*callback_t)(char *);

void callback_test(char *str) {
    printf("%s\n", str);
    return;
}

void callback_main(callback_t cb) {
    cb("hello");
    return;
}

int main(int argc, char const *argv[]) {
    callback_main((callback_t)(&callback_test));
    return 0;
}
Copy the code

Deformation of a

The main function has a strong pointer type, can not strong pointer? The answer is yes, any of the Pointers in C can be strong with each other, as long as you know what you’re doing. Callback_main (&callback_test); OK, too.

The complete code is as follows:

#include <stdio.h>

typedef void (*callback_t)(char *);

void callback_test(char *str) {
  printf("%s\n", str);
  return;
}

void callback_main(callback_t cb) {
  cb("hello");
  return;
}

int main(int argc, char const *argv[]) {
  callback_main(&callback_test);
  return 0;
}
Copy the code

Deformation of the second

A new type called callback_t is defined at the top. So can the answer. All you need to do is change the callback_main input definition. Void callback_main(void (*cb)(char *))

The complete code is as follows:

#include <stdio.h>

typedef void (*callback_t)(char *);

void callback_test(char *str) {
  printf("%s\n", str);
  return;
}

void callback_main(void (*cb)(char *)) {
  cb("hello");
  return;
}

int main(int argc, char const *argv[]) {
  callback_main(&callback_test);
  return 0;
}
Copy the code

After class on

I don’t know if there are any other variations, but next time think about creating something that mimics Javascript closures and works with Callback.

Bellard’s program for tinkering with prime numbers also wants to tinker with it, and there are some more sophisticated algorithms in it, fast Fourier transforms and things like that, that he wants to do. His Code is right here, it’s just a little blob, and it compiles at 220 megabytes, which is incredible.