const qualifier usage

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sorav Bansal

    #1

    const qualifier usage

    const int *pci ; //pci points to const int
    int *const cpi ; //cpi is a const pointer to int

    int const *dunno ; //I don't know what this declaration means??

    Could somebody help.

    thanks
  • Eric Sosman

    #2
    Re: const qualifier usage



    Sorav Bansal wrote:[color=blue]
    > const int *pci ; //pci points to const int
    > int *const cpi ; //cpi is a const pointer to int
    >
    > int const *dunno ; //I don't know what this declaration means??[/color]

    Same as the first one: `dunno' is a pointer to an
    unmodifiable `int'. (Unmodifiable by means of `dunno',
    at any rate -- the target `int' may be modifiable by
    other means.)

    --
    Eric.Sosman@sun .com

    Comment

    • Sorav Bansal

      #3
      Re: const qualifier usage

      Eric Sosman wrote:[color=blue]
      >
      > Sorav Bansal wrote:
      >[color=green]
      >>const int *pci ; //pci points to const int
      >>int *const cpi ; //cpi is a const pointer to int
      >>
      >>int const *dunno ; //I don't know what this declaration means??[/color]
      >
      >
      > Same as the first one: `dunno' is a pointer to an
      > unmodifiable `int'. (Unmodifiable by means of `dunno',
      > at any rate -- the target `int' may be modifiable by
      > other means.)
      >[/color]

      thanks!

      Comment

      • Chris Croughton

        #4
        Re: const qualifier usage

        On Wed, 13 Jul 2005 11:51:02 -0700, Sorav Bansal
        <sbansal@stanfo rd.edu> wrote:
        [color=blue]
        > const int *pci ; //pci points to const int
        > int *const cpi ; //cpi is a const pointer to int
        >
        > int const *dunno ; //I don't know what this declaration means??[/color]

        It means the same as the first form, a pointer to constant int. Why it
        is an allowed form I don't know, perhaps someone was having a bad day
        and wanted to make things confusing...

        $ cdecl
        int const *dunno ;
        declare dunno as pointer to int const;

        Chris C

        Comment

        • E. Robert Tisdale

          #5
          Re: const qualifier usage

          Sorav Bansal wrote:
          [color=blue]
          > const int* pci; // int const* pci;[/color]

          const int ci = 0; // int const ci = 0;
          pci = &ci; // ok!
          *pci = 42; // error!

          int i;[color=blue]
          > int* const cpi = &i;[/color]

          *cpi = 0; // ok!
          cpi = NULL; // error!
          [color=blue]
          > int const* dunno; // const int* dunno;[/color]

          dunno = &ci; // ok!
          *dunno = 42; // error!

          Comment

          • Old Wolf

            #6
            Re: const qualifier usage

            Chris Croughton wrote:[color=blue]
            > On Wed, 13 Jul 2005 11:51:02 -0700, Sorav Bansal
            > <sbansal@stanfo rd.edu> wrote:
            >[color=green]
            > > const int *pci ; //pci points to const int
            > > int *const cpi ; //cpi is a const pointer to int
            > >
            > > int const *dunno ; //I don't know what this declaration means??[/color]
            >
            > It means the same as the first form, a pointer to constant int. Why it
            > is an allowed form I don't know, perhaps someone was having a bad day
            > and wanted to make things confusing...[/color]

            Do you mean, why is "const int *pci" an allowed form?
            In all cases except for that one, 'const' comes immediately
            to the right of the type that it's modifying.

            Comment

            • Lawrence Kirby

              #7
              Re: const qualifier usage

              On Wed, 13 Jul 2005 14:38:03 -0700, Old Wolf wrote:
              [color=blue]
              > Chris Croughton wrote:[color=green]
              >> On Wed, 13 Jul 2005 11:51:02 -0700, Sorav Bansal
              >> <sbansal@stanfo rd.edu> wrote:
              >>[color=darkred]
              >> > const int *pci ; //pci points to const int
              >> > int *const cpi ; //cpi is a const pointer to int
              >> >
              >> > int const *dunno ; //I don't know what this declaration means??[/color]
              >>
              >> It means the same as the first form, a pointer to constant int. Why it
              >> is an allowed form I don't know, perhaps someone was having a bad day
              >> and wanted to make things confusing...[/color]
              >
              > Do you mean, why is "const int *pci" an allowed form?[/color]

              Well it is the form a lot of programmers prefer.
              [color=blue]
              > In all cases except for that one, 'const' comes immediately
              > to the right of the type that it's modifying.[/color]

              What you refer to as "all cases" is only a single case: const may follow a
              * (pointer) declarator. The language makes an important distinction
              between declaration specifiers (static, char, unsigned, const, struct
              etc.) and declarators (pointer, array and function derivations). It makes
              sense to be able to distinguish between the two when reading a
              declaration, the issue here being that const (and volatile) can appear in
              both. Of course there is potential for real nastiness such as

              int static short volatile unsigned

              Lawrence



              Comment

              • Chris Croughton

                #8
                Re: const qualifier usage

                On 13 Jul 2005 14:38:03 -0700, Old Wolf
                <oldwolf@inspir e.net.nz> wrote:
                [color=blue]
                > Chris Croughton wrote:[color=green]
                >> On Wed, 13 Jul 2005 11:51:02 -0700, Sorav Bansal
                >> <sbansal@stanfo rd.edu> wrote:
                >>[color=darkred]
                >> > const int *pci ; //pci points to const int
                >> > int *const cpi ; //cpi is a const pointer to int
                >> >
                >> > int const *dunno ; //I don't know what this declaration means??[/color]
                >>
                >> It means the same as the first form, a pointer to constant int. Why it
                >> is an allowed form I don't know, perhaps someone was having a bad day
                >> and wanted to make things confusing...[/color]
                >
                > Do you mean, why is "const int *pci" an allowed form?[/color]

                No, I mean why is "int const" allowed.
                [color=blue]
                > In all cases except for that one, 'const' comes immediately
                > to the right of the type that it's modifying.[/color]

                Which is itself confusing.

                Chris C

                Comment

                Working...