Article View: gnu.gcc.help
Article #42569Re: Problem with const pointer parameters and typedef
From: Steve Clamage
Date: Sun, 26 Nov 1995 00:00
Date: Sun, 26 Nov 1995 00:00
46 lines
1663 bytes
1663 bytes
pld@syd.au.swissbank.com (Phillip Drury) writes:
>I have come across this interesting problem with const pointer parameters
>and typedefs ...
>typedef int *pINT;
>void
>Test(int* param)
> {
> }
>void
>Func1(const int* param)
> {
> Test(param); /* Compiler complains const int* passed to int* parameter */
> }
>Func2(const pINT param)
> {
> Test(param); /* Compiler does not complain */
> }
The compiler is correct.
A typedef is not a macro. It gives a name to a type. In this case,
pINT is the name given to pointer-to-int.
When you write
const pINT param
it is the same as
pINT const param
Each version declares param to be const, having type pINT. Type
pINT is type pointer to (non-const) int. The "const" in the complete
declaration applies to param, the declarator.
That is quite different from, and incompatible with,
const int* param
or
int const* param
each of which declares param to be a pointer to const int. The "const"
applies to the declarator, which is "*". Notice the declaration using
the typedef does not have a "*".
--
Steve Clamage, stephen.clamage@eng.sun.com
Technical Details
Message-ID:
<gnusenet490pdf$eeh@engnews1.eng.sun.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<1995Nov23.012520.24896@il.us.swissbank.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 11318c,17d5a0531f394683 X-Google-Attributes: gid11318c,public From: clamage@eng.SUn.COM (Steve Clamage) Subject: Re: Problem with const pointer parameters and typedef Date: 1995/11/26 Message-ID: <gnusenet490pdf$eeh@engnews1.eng.sun.com> X-Deja-AN: 120166880 distribution: gnu sender: daemon@cis.ohio-state.edu references: <1995Nov23.012520.24896@il.us.swissbank.com> organization: Sun Microsystems Inc., Mountain View, CA newsgroups: gnu.gcc.help