M
Michael
Hello,
I've a couple of (automatically) created functions that take
2-dimensional array as inputs, like
void getMatrix( double M[2][2] )
{
M[0][0] = 1;
M[0][1] = 0;
M[1][0] = 0;
M[1][1] = 1;
}
The size of the matrices varies: 2x2, 3x2, etc. but is
always defined in the function header. I don't need all
matrices simultaneously. Is it allowed to pass an array that
has larger dimensions (i.e. those of the largest matrix)
than specified in the function header? Currently I tried this:
============================================================
#include <stdio.h>
void getMatrix( double M[2][2] );
void getMatrix( double M[2][2] )
{
M[0][0] = 1;
M[0][1] = 0;
M[1][0] = 0;
M[1][1] = 1;
};
int main( void )
{
double A[3][3];
void (*fnc)(double M[][]) = getMatrix;
(*fnc)( A );
printf("[ %.1f, %.1f]\n", A[0][0], A[0][1]);
printf("[ %.1f, %.1f]\n", A[0][2], A[1][0]);
return 0;
}
============================================================
Although the compiler takes it and the executable prints the
desired result, it seems a bit odd to me. The function
pointer seems to suppress the information of the array size.
Without this intermediate step the compiler throws an error
(which might be already a hint that the code is not valid).
The only other way a can think of is to create a bunch of
buffers and decide at each call which I have to use.
Regards
Michael
I've a couple of (automatically) created functions that take
2-dimensional array as inputs, like
void getMatrix( double M[2][2] )
{
M[0][0] = 1;
M[0][1] = 0;
M[1][0] = 0;
M[1][1] = 1;
}
The size of the matrices varies: 2x2, 3x2, etc. but is
always defined in the function header. I don't need all
matrices simultaneously. Is it allowed to pass an array that
has larger dimensions (i.e. those of the largest matrix)
than specified in the function header? Currently I tried this:
============================================================
#include <stdio.h>
void getMatrix( double M[2][2] );
void getMatrix( double M[2][2] )
{
M[0][0] = 1;
M[0][1] = 0;
M[1][0] = 0;
M[1][1] = 1;
};
int main( void )
{
double A[3][3];
void (*fnc)(double M[][]) = getMatrix;
(*fnc)( A );
printf("[ %.1f, %.1f]\n", A[0][0], A[0][1]);
printf("[ %.1f, %.1f]\n", A[0][2], A[1][0]);
return 0;
}
============================================================
Although the compiler takes it and the executable prints the
desired result, it seems a bit odd to me. The function
pointer seems to suppress the information of the array size.
Without this intermediate step the compiler throws an error
(which might be already a hint that the code is not valid).
The only other way a can think of is to create a bunch of
buffers and decide at each call which I have to use.
Regards
Michael