Article View: comp.std.c
Article #1296const arrays in prototypes
From: Peter Montgomery
Date: Fri, 25 May 1990 17:37
Date: Fri, 25 May 1990 17:37
40 lines
1789 bytes
1789 bytes
/*
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@MATH.UCLA.EDU
Department of Mathematics, UCLA, Los Angeles, CA 90024
Technical Details
Message-ID:
<2781@sunset.MATH.UCLA.EDU>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!mailrus!cs.utexas.edu!sdd.hp.com!elroy.jpl.nasa.gov!ucla-cs!math.ucla.edu!pmontgom@euphemia.math.ucla.eduShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!mailrus!cs.utexas.edu!sdd.hp.com!elroy.jpl.nasa.gov!ucla-cs!math.ucla.edu!pmontgom@euphemia.math.ucla.edu From: pmontgom@euphemia.math.ucla.edu (Peter Montgomery) Newsgroups: comp.std.c Subject: const arrays in prototypes Message-ID: <2781@sunset.MATH.UCLA.EDU> Date: 25 May 90 17:37:13 GMT Sender: news@MATH.UCLA.EDU Organization: UCLA Mathematics Dept. Lines: 40