i have this issue in C. Let's say, i have a struct like so:
struct Writer {
char buffer[10];
void (*writeFunction)(uint8_t*, size_t);
}
This struct has methods that use a user given writer function pointer to dump the values, something like so:
struct Writer writer;
FILE* file = fopen("test.txt", "wb");
void writeToFile(uint8_t* content, size_t size)
{
fwrite(content, 1, size, file);
}
write.writeFunction = writeToFile;
This works fine, when the user writes the write functions and passes them manually. Is there however a way to automate this, so that i can say, for every new Writer struct, a new writeFunction pointer with a from-user-given-filepath, is generated. I tried, but i keep coming to C++ lambda functions or needing the this pointer from C++.
Expected is something like:
void generate(struct Writer* writer, const char* filename)
{
// somehow, writer->writerfunction needs to point to a
writeToFile function like above
}
Thank you.