const arrays in prototypes
Peter Montgomery
pmontgom at euphemia.math.ucla.edu
Sat May 26 03:37:13 AEST 1990
/*
When declaring a pointer to a function, careful use of const
lets us distinguish whether the function is allowed to modify the
pointer and/or the data. For example
*/
void proc1 ( int *p1, /* non-const pointer to non-const int */
const int *p2, /* non-const pointer to const int */
int *const p3, /* const pointer to non-const int */
const int *const p4) /* const pointer to const int */
{
p1++;
p2++;
p3++; /* illegal modification of pointer */
p4++; /* illegal modification of pointer */
*p1 = 1;
*p2 = 2; /* illegal modification of data */
*p3 = 3;
*p4 = 4; /* illegal modification of data */
}
/*
As desired, gcc 1.37.1 issues warnings for the four lines
marked "illegal".
Suppose instead that I am passing an array argument. If I
put "const" before the declaration, then I am stating that the data
will not be modified. How do I specify that the pointer to the array
will not be altered within the subroutine? For example,
*/
void DISALLOW(const int mat[2][2])
{
mat++; /* I want to forbid this usage. */
mat[0][1] = 4; /* Gcc 1.37.1 properly warns about this line. */
}
--
--------
Peter Montgomery
pmontgom at MATH.UCLA.EDU
Department of Mathematics, UCLA, Los Angeles, CA 90024
More information about the Comp.std.c
mailing list