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

Discussion in 'C Programming' started by Paulo, May 26, 2010.

  1. Paulo

    Paulo Guest

    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.
     
    Paulo, May 26, 2010
    #1
    1. Advertising

  2. Paulo

    Ben Pfaff Guest

    Paulo <> writes:

    > 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;}}}
     
    Ben Pfaff, May 26, 2010
    #2
    1. Advertising

  3. Paulo <> writes:

    > 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.
     
    Ben Bacarisse, May 27, 2010
    #3
  4. On Wed, 26 May 2010, Paulo wrote:

    > 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
     
    Ersek, Laszlo, May 27, 2010
    #4
  5. Ben Bacarisse <> writes:
    > Paulo <> writes:

    <snip>
    >> typedef int TA[10];
    >> typedef int * TP;
    >>
    >> void foo(
    >> const TA px,
    >> const TP py )
    >> {
    >> }

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

    --
    Ben.
     
    Ben Bacarisse, May 27, 2010
    #5
  6. Paulo

    Paulo Guest

    Re: How is const applied to a pointer declared in a functionparameter

    On 27 May, 00:23, "Ersek, Laszlo" <> wrote:
    > On Wed, 26 May 2010, Paulo wrote:
    > > 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
     
    Paulo, Jun 2, 2010
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Polanski24
    Replies:
    1
    Views:
    563
    Rolf Magnus
    Apr 11, 2005
  2. coala
    Replies:
    3
    Views:
    397
    coala
    Sep 6, 2006
  3. coala
    Replies:
    1
    Views:
    606
    Victor Bazarov
    Sep 6, 2006
  4. Javier
    Replies:
    2
    Views:
    602
    James Kanze
    Sep 4, 2007
  5. Old Wolf

    Same pointer parameter const and non-const

    Old Wolf, Nov 3, 2009, in forum: C Programming
    Replies:
    3
    Views:
    552
    Ben Bacarisse
    Nov 4, 2009
Loading...

Share This Page