Article View: gnu.gcc.help
Article #42572Re: Problem with const pointer parameters and typedef
From: Dietmar Kuehl
Date: Sun, 26 Nov 1995 00:00
Date: Sun, 26 Nov 1995 00:00
64 lines
2855 bytes
2855 bytes
Hi,
Phillip Drury (pld@syd.au.swissbank.com) wrote:
: 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)
This is equivalent to 'Func2(int * const param)'. Actually, confusion
like this led me to always put the 'const' at the most right position
where it could be placed without changing the semantic: This applies
basically to the pointer cases where the following two declarations
have the same semantics: 'T const *' (my preferred style) and 'const T
*'. Analogous with references.
: {
: Test(param); /* Compiler does not complain */
: }
:
: Now can anyone explain to me why the compiler does not complain about
: the second case because "const int* param" should be exactly the same
: as "const pINT param" as there exists the typedef int *pINT.
Write the types as I suggested (i.e. exchange the 'const' with the first
typename found) and you see the difference:
1. 'const pINT' == 'pINT const' == 'int * const'
2. 'const int *' == 'int const *' != 'int * const'
For 1. the pointer is constant. This doesn't matter in this case since
the pointer is passed by value. In 2. the objects pointed to are
constant. This does matter because you try to convert a pointer to
constant objects to a pointer to non-constant objects.
: "A typedef declaration attributes a type to each name among its
: declarators in the usual way. Thereafter, each such typedef name
: is syntactically equivalent to a type specifier keyword for the
: associated type"
:
: This statement leads me to believe that the above behaviour exhibited by
: these two compilers is wrong.
As explained above, I don't think so.
:
: So what is the story. Is this a compiler bug or am I missing something
: and if so what is the fix. I do not wish to resort to #define pINT *int,
: if I can at all avoid it.
You can use 'typedef int const *pcINT' (pointer to 'const int') in
addition to your 'pINT' (pointer to 'int').
--
dietmar.kuehl@uni-konstanz.de
http://www.informatik.uni-konstanz.de/~kuehl
I am a realistic optimist - that's why I appear to be slightly pessimistic
Technical Details
Message-ID:
<gnusenet491vek$8cb@eurybia.rz.uni-konstanz.de>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: kuehl@uzwil.ai.mit.EDU (Dietmar Kuehl) Subject: Re: Problem with const pointer parameters and typedef Date: 1995/11/26 Message-ID: <gnusenet491vek$8cb@eurybia.rz.uni-konstanz.de> X-Deja-AN: 120166883 distribution: gnu sender: daemon@cis.ohio-state.edu references: <1995Nov23.012520.24896@il.us.swissbank.com> organization: Fakultt fr Mathematik und Informatik reply-to: dietmar.kuehl@uni-konstanz.de newsgroups: gnu.gcc.help