R
Rouben Rostamian
The main() function in the following code defines an m by n
matrix, assigns value(s) to its elements, then passes the matrix
to function foo().
For whatever it's worth, I have declared foo() so as to make
it treat its first argument as a "read-only" object, that is,
foo() can read but not alter the matrix.
I have a problem, however, with /calling/ foo. If I call
foo as foo(a,m,n), my compiler (gcc) complains about:
try.c:22: warning: passing argument 1 of 'foo' from
incompatible pointer type
I can silence the warning by using a cast:
foo((double const * const *)a, m, n);
I wonder, however, if there is a way to declare foo() to
make the cast unnecessary but at the same time tell it that
its argument is a read-only object.
%---% sample code: try.c %-----------------------------------%
#include <stdlib.h>
void foo(double const * const *a, size_t m, size_t n)
{
a[0] = NULL; /* illegal */
a[0][0] = 4.0; /* illegal */
2.0*a[0][0]; /* legal */
}
int main(void)
{
double **a;
size_t m=3, n=5;
size_t i;
a = malloc(m * sizeof *a);
for (i=0; i<m; i++)
a = malloc(n * sizeof *a);
a[0][0] = 1.0;
foo((double const * const *)a, m, n); /* really want foo(a,m,n); */
return 0;
}
%---% end sample code %--------------------------------------%
matrix, assigns value(s) to its elements, then passes the matrix
to function foo().
For whatever it's worth, I have declared foo() so as to make
it treat its first argument as a "read-only" object, that is,
foo() can read but not alter the matrix.
I have a problem, however, with /calling/ foo. If I call
foo as foo(a,m,n), my compiler (gcc) complains about:
try.c:22: warning: passing argument 1 of 'foo' from
incompatible pointer type
I can silence the warning by using a cast:
foo((double const * const *)a, m, n);
I wonder, however, if there is a way to declare foo() to
make the cast unnecessary but at the same time tell it that
its argument is a read-only object.
%---% sample code: try.c %-----------------------------------%
#include <stdlib.h>
void foo(double const * const *a, size_t m, size_t n)
{
a[0] = NULL; /* illegal */
a[0][0] = 4.0; /* illegal */
2.0*a[0][0]; /* legal */
}
int main(void)
{
double **a;
size_t m=3, n=5;
size_t i;
a = malloc(m * sizeof *a);
for (i=0; i<m; i++)
a = malloc(n * sizeof *a);
a[0][0] = 1.0;
foo((double const * const *)a, m, n); /* really want foo(a,m,n); */
return 0;
}
%---% end sample code %--------------------------------------%