I'm trying to write a function that allows me to initialise every element of a matrix with a given value. I'd like for this function to be as generic as possible, meaning that it would be able to treat matrices of any data type (float, char, etc). The function would obviously need as one of the argument the value that the user wants the elements of the matrix to be initialised with. Since this value could be of any kind of data type, I don't know how to go about this. Standard functions such as printf and scanf are able to accept arguments of any kind thanks to their use of format specifiers (%d, %f, etc). This got me wondering: how and is it even possible to use format specifiers in a programmer-defined function? Ideally, my function would look something like this:
void initMatrix(void*** matrixToInit, int nRows, int nCols, const char format, void(?) value)
So that upon calling it I would have to write something like:
char matrixOfAs[3][3];
initMatrix(&matrixOfAs, 3, 3, "%c", 'A');
Is something like this feasible? Are there other solutions to this problem?