Article View: comp.std.c
Article #1303Re: const arrays in prototypes
From: Karl Heuer
Date: Sat, 26 May 1990 21:51
Date: Sat, 26 May 1990 21:51
29 lines
1680 bytes
1680 bytes
>In article <2781@sunset.MATH.UCLA.EDU> pmontgom@euphemia.math.ucla.edu (Peter Montgomery) writes:
>>Suppose instead that I am passing an array argument.
You can't. So I'll assume you meant "suppose I am passing a pointer and using
the pointer-argument syntax that resembles an array declaration."
>>How do I specify that the pointer to the array will not be altered within
>>the subroutine?
You can't. Use the equivalent pointer syntax instead, then put the const
keyword in the right place.
>>For example,
>> void DISALLOW(const int mat[2][2]) {...}
The misdeclaration "int mat[2][2]" is silently converted to "int (*mat)[2]",
so what you want is
void DISALLOW(int (*const mat)[2]) {...}
or, if you intend both the pointer and the data to be non-modifiable,
void DISALLOW(int const (*const mat)[2]) {...}
(the first "const" may also appear before the type).
In article <1356@crabcake> zhu@crabcake.cs.jhu.edu (Benjamin Zhu) writes:
>Use the following prototype instead:
> void DISALLOW(const int ** const mat)
Sorry, that's not equivalent. You've changed the semantics of his function.
Karl W. Z. Heuer (karl@ima.ima.isc.com or harvard!ima!karl), The Walking Lint
Technical Details
Message-ID:
<16733@haddock.ima.isc.com>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!snorkelwacker!spdcc!ima!haddock!karlReferences:
<2781@sunset.MATH.UCLA.EDU> <1356@crabcake>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!snorkelwacker!spdcc!ima!haddock!karl From: karl@haddock.ima.isc.com (Karl Heuer) Newsgroups: comp.std.c Subject: Re: const arrays in prototypes Message-ID: <16733@haddock.ima.isc.com> Date: 26 May 90 21:51:54 GMT References: <2781@sunset.MATH.UCLA.EDU> <1356@crabcake> Reply-To: karl@haddock.ima.isc.com (Karl Heuer) Organization: Interactive Systems, Cambridge, MA 02138-5302 Lines: 29