It is necessary to apply to all elements of the matrix a certain function passed by the pointer.

Do not really understand what kind of function is meant here?

  • “What kind of function” is just any function to which the pointer passed to you points - andreymal pm
  • @andreymal can you give an example? - walde pm
  • int matrix[N][M]; void f(int); The task is to write something like void g(int m[][M], void (*func)(int)) , in which func is applied to all the elements of the matrix so that it can be called as g(matrix,f) - Harry
  • @Harry I will try thanks - walde 2:13 pm

1 answer 1

I see all this something like this, if of course I correctly understood the question

 #include <stdio.h> #include <math.h> void somedo1(void *_x) { int *x = _x; *x = *x * *x; } void somedo2(void *_x) { double *x = _x; *x = log(*x); } void func(void *matrix, size_t nmemb, size_t size, void (*f)(void *)) { char *m = matrix; for (size_t i = 0; i < nmemb; i++) f(&m[i * size]); } int main(void) { int matrix1[4][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}}; double matrix2[2][2] = {{1.2, 2.2}, {3.3, 4.4}}; func(matrix1, 12, sizeof(*matrix1[0]), somedo1); func(matrix2, 4, sizeof(*matrix2[0]), somedo2); for (size_t i = 0; i < 4; i++, puts("")) for (size_t j = 0; j < 3; j++) printf("%4d", matrix1[i][j]); puts(""); for (size_t i = 0; i < 2; i++, puts("")) for (size_t j = 0; j < 2; j++) printf("%6.2lf", matrix2[i][j]); return 0; } 
  • Yes, it seems to be true - walde