How is const applied to a pointer declared in a function parameter

P

Paulo

When is a pointer a "const pointer" rather than a "pointer to
const" ?

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.
 
B

Ben Pfaff

Paulo said:
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.
 
B

Ben Bacarisse

Paulo said:
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?
 
E

Ersek, Laszlo

When is a pointer a "const pointer" rather than a "pointer to
const" ?

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.


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
 
B

Ben Bacarisse

Ben Bacarisse said:
Paulo <[email protected]> writes:
typedef int TA[10];
typedef int * TP;

void foo(
const TA px,
const TP py )
{
}
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.

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.
 
P

Paulo

When is a pointer  a "const pointer" rather than a "pointer to
const" ?
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.

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":

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top