Function Pointers
I am a Student, who finds beauty in simple things. I like to teach sometimes.
Search for a command to run...
I am a Student, who finds beauty in simple things. I like to teach sometimes.
No comments yet. Be the first to comment.
In this series, I will learn and write on topics related to advanced C, inspired from https://www.youtube.com/@cacharle
There is a distinct heaviness that descends when life proceeds smoothly on the surface. Externally, everything may be stable, yet the desire to die can persist not because of tragedy, but because of a realization regarding the future. If the destinat...
"I must not fear abstraction. Abstraction is the mind-killer. Abstraction is the little-death that brings total obliteration. I will face my abstraction. I will permit it to pass over me and through me. And when it has gone past I will turn the inner...
Ever wondered how the Python or JavaScript code you write actually makes your computer's fans spin up? How do abstract commands like print("Hello, World!") get turned into physical actions? The magic lies in a fundamental, deeply interconnected relat...
I'll speed through setting up an ASIC synthesis flow for the Ibex RISC-V core using entirely open-source tools. Tools Python 3.12.8 (for environment management) Yosys (logic synthesis) sv2v (SystemVerilog to Verilog conversion) OpenSTA (static ti...
Imagine this scene: A dimly lit room, humming with the quiet thrum of advanced technology. Three alien scientists are hunched over a console, staring intently at a string of data flashing across a screen: 0101010100... Alien Scientist #1: "It isn't r...
C is already so hard and why such function pointers???
They are interesting and they help you to get separateed from soydevs who use chatgpt and genAI to code. May the legacy of legacy programmers Rise!
int func(int x, int y) {
return x + y;
}
// Declaring a Function Pointer
int (*ptr_name) (int, int) = func;
When we write a fucntion like this, it is converted into machine code (binary boop boop beep) by the compiler at compile time. A fucntion ultimately is just an address in memory where there exists some code. So, Techinically, we can make and use function pointers for different purposes.
Suppose we do something like
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
bool is_even(int x){
return x % 2 == 0;
}
void print_conditional(int x[10], bool (*predicate) (int)) {
for (int i = 0; i < 10; i++) {
if (predicate(x[i])) {
printf("%d\n", x[i]);
}
}
}
int main() {
int x[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
print_conditional(x, is_even);
}
Gives Output
batman@batcave ~/C/c (master)> goku fp.c
2
4
6
8
10
batman@batcave ~/C/c (master)>
Heck with usability, This is cool that i can write k different predicate check functions, which can be passed into the print_conditional without changing the core logic. Write good test cases, or making very generic abstract functions… Do anything. Its C. See?