In the following code example, the mainstream compilers which I have
used seem to interpret "px" as a "pointer to const int" and "py" as a
"const pointer to int".
typedef int TA[10];
typedef int * TP;
void foo(
const TA px,
const TP py )
{
}
Is this correct implementation ?
I would have expected both to be of type "const pointer to int".
Can you point me to a reference in ISO-C please.
> When is a pointer a "const pointer" rather than a "pointer to
> const" ?
"const int *" is a pointer to a const int.
"int *const" is a const pointer to a (non-const) int.
The important part is the relative order of "*" and "const".
Thus, "int const *" is also a pointer to a const int.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
> When is a pointer a "const pointer" rather than a "pointer to
> const" ?
See the reply from another Ben.
> In the following code example, the mainstream compilers which I have
> used seem to interpret "px" as a "pointer to const int" and "py" as a
> "const pointer to int".
>
> typedef int TA[10];
> typedef int * TP;
>
> void foo(
> const TA px,
> const TP py )
> {
> }
>
> Is this correct implementation ?
I'd say no.
> I would have expected both to be of type "const pointer to int".
Me too.
> Can you point me to a reference in ISO-C please.
py is simple, I think, but for px you'd start by looking at 6.7.5.3 p7.
You have to read up on typedef names and on const qualifiers as well but
in the end I think you have to conclude that px should have type
constant pointer to int (const int *) as you originally thought.
By the way, how do you know your compiler treats px as const pointer to
int? Does foo try to assign to px?
--
Ben.
6.7.3 Type qualifiers, p8:
----v----
If the specification of an array type includes any type qualifiers, the
element type is so-qualified, not the array type. If the specification of
a function type includes any type qualifiers, the behavior is undefined.
^116)
----^----
Footnote 116: "Both of these can occur through the use of typedefs."
In your example, "const TP py" const-qualifies "py", which is of scalar
(pointer) type. However, "const TA px" const-qualfies the element type,
since "px" has array type. Then the decl. of "px" is adjusted from "array
of const int" to "pointer to const int":
6.7.5.3 Function declarators (including prototypes), p7:
----v----
A declaration of a parameter as ``array of type'' shall be adjusted to
``qualified pointer to type'', where the type qualifiers (if any) are
those specified within the [ and ] of the array type derivation. [...]
----^----
"[10]" contains no type qualifiers, so the (adjusted) declaration of "px"
as a pointer-to-const-int is not qualified itself.
Some variations:
void
foo(
const int v1a[const 10],
const int * const v1p,
const int v2a[10],
const int *v2p,
int v3a[const 10],
int * const v3p
);
NB. the following would be a constraint violation according to 6.7.5.2
Array declarators, p1:
typedef int TA[const 10]; /* invalid code */
C89 also doesn't define "v1a" and "v3a".
Cheers,
lacos
The type written in C is correct (const int *). I over-edited that
paragraph so much that I ended up with the wrong type in the English
version! I hope that was obvious from the "as you originally thought"
since you thought, correctly, that px has type "pointer to const int".
Also, I go on to ask why you think the type is not as you expected:
> By the way, how do you know your compiler treats px as const pointer to
> int? Does foo try to assign to px?
so you might be ably to spot what I intended.
Annoying -- like missing out a "not" it can really mess up a thread so I
hope I've corrected it soon enough.
--
Ben.
This assumes that the const qualification is applied *before* "px" is
adjusted to pointer type rather than *after*.
I'm not sure how that assumption can be made.
>
> 6.7.5.3 Function declarators (including prototypes), p7:
>
> ----v----
> A declaration of a parameter as ``array of type'' shall be adjusted to
> ``qualified pointer to type'', where the type qualifiers (if any) are
> those specified within the [ and ] of the array type derivation. [...]
> ----^----
>
> "[10]" contains no type qualifiers, so the (adjusted) declaration of "px"
> as a pointer-to-const-int is not qualified itself.
>
> Some variations:
>
> void
> foo(
> const int v1a[const 10],
> const int * const v1p,
>
> const int v2a[10],
> const int *v2p,
>
> int v3a[const 10],
> int * const v3p
> );
>
> NB. the following would be a constraint violation according to 6.7.5.2
> Array declarators, p1:
>
> typedef int TA[const 10]; /* invalid code */
>
> C89 also doesn't define "v1a" and "v3a".
>
> Cheers,
> lacos
Thanks for your help.
Paulo