const arrays in prototypes

Karl Heuer karl at haddock.ima.isc.com
Sun May 27 07:51:54 AEST 1990


>In article <2781 at sunset.MATH.UCLA.EDU> pmontgom at 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 at crabcake> zhu at 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 at ima.ima.isc.com or harvard!ima!karl), The Walking Lint



More information about the Comp.std.c mailing list