pointer in C++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob Keith

    pointer in C++

    I am a beginner. So this question could be very stupid.

    I am not very clear about the pointer in C++, would anyone let me know
    the difference of the following usage of pointer.
    int* a
    int *a
    int &a
    int& a
    int*& a

    Many thanks.
  • Stuart Golodetz

    #2
    Re: pointer in C++

    "Bob Keith" <structuralvibr ation@yahoo.com > wrote in message
    news:b70f4ea8.0 306271048.7b5d3 2dd@posting.goo gle.com...[color=blue]
    > I am a beginner. So this question could be very stupid.
    >
    > I am not very clear about the pointer in C++, would anyone let me know
    > the difference of the following usage of pointer.
    > int* a // a is a pointer to an int
    > int *a // a is again a pointer to an int
    > int &a // a is a reference to an int
    > int& a // a is again a reference to an int
    > int*& a // a is a reference to a pointer to an int[/color]

    HTH,

    Stuart.
    [color=blue]
    > Many thanks.[/color]


    Comment

    • Victor Bazarov

      #3
      Re: pointer in C++

      "Bob Keith" <structuralvibr ation@yahoo.com > wrote...[color=blue]
      > I am a beginner. So this question could be very stupid.
      >
      > I am not very clear about the pointer in C++, would anyone let me know
      > the difference of the following usage of pointer.
      > int* a
      > int *a[/color]

      The two declarations above are the same and declare 'a'
      a pointer to an int.
      [color=blue]
      > int &a
      > int& a[/color]

      The two declarations above are the same and declare 'a'
      a reference to an int (no pointers here).
      [color=blue]
      > int*& a[/color]

      This declaration declares 'a' a reference to a pointer to
      an int.

      All three last declarations require initialisation if they
      are also definitions. Sounds convoluted? It is, sort of.
      If your declaration is in the namespace scope or in a function
      scope, they are also definitions. And any definition of
      a reference requires an initialiser.

      int *& rpa; // error - lacks initialiser (namespace scope)
      int main()
      {
      int& a; // error - lacks initialiser (function scope)
      }
      [color=blue]
      >
      > Many thanks.[/color]

      Many welcomes.

      Victor


      Comment

      • Ron Natalie

        #4
        Re: pointer in C++


        "Bob Keith" <structuralvibr ation@yahoo.com > wrote in message news:b70f4ea8.0 306271048.7b5d3 2dd@posting.goo gle.com...[color=blue]
        > I am a beginner. So this question could be very stupid.
        >
        > I am not very clear about the pointer in C++, would anyone let me know
        > the difference of the following usage of pointer.
        > int* a
        > int *a
        > int &a
        > int& a
        > int*& a
        >[/color]
        Half of those aren't pointers.
        The arrangement of the white space is not significant.


        Comment

        • Sin

          #5
          Re: pointer in C++

          > I am not very clear about the pointer in C++, would anyone let me know[color=blue]
          > the difference of the following usage of pointer.[/color]

          Sure

          [color=blue]
          > int* a
          > int *a[/color]

          Same exact thing. Should it be on a, or on int? It doensn't change a thing
          as far as the compiler is concerned and for clarity there are arguments on
          both sides. The important thing to know is that "int *a, b;" will declare a
          int pointer (a) and an integer (b), rather than two pointers...

          As for what a pointer is, it's simply a placeholder for an address...

          Ex:

          int v= 12;
          int *a= &v;
          int *b= &v
          int *c= &v;

          *a= 13;
          // At this point, v == 13, *a == 13, *b == 13 and *c == 13

          [color=blue]
          > int &a
          > int& a[/color]

          Again, both are the same. They are NOT pointers though they are references.
          A reference is similar to a pointer, but it's actually just a variable that
          shares the same address as the object it's initialized with. It makes their
          use easier for the non initiated, I guess, but it lacks the possibility of
          being set to nothing (NULL).

          Ex:

          int v= 12;
          int &a= v;
          int &b= v;
          int &c= v;

          a= 13;
          // At this point, v == 13, a == 13, b == 13 and c == 13

          [color=blue]
          > int*& a[/color]

          That would be a reference to a pointer...

          Ex:

          int v= 12;
          int &r= v;
          int *p= &r;
          int *&rp= p;

          *rp= 13;

          // At this point, v == 13, r == 13, *p == 13, **rp == 13


          In my opinion references should be avoided unless necessary. I mostly do C
          with occasionnal C++ and I can count on the fingers of one hand the times I
          had to use references in the past 5 years...

          Alex.


          Comment

          • Ron Natalie

            #6
            Re: pointer in C++


            "Howard" <alicebt@hotmai l.com> wrote in message news:bdig1c$f3v @dispatch.conce ntric.net...[color=blue]
            >
            > "Ron Natalie" <ron@sensor.com > wrote in message
            > news:Kdydnbiyys hyBmGjXTWQlg@gi ganews.com...[color=green]
            > >[/color]
            >[color=green][color=darkred]
            > > > int* a
            > > > int *a
            > > > int &a
            > > > int& a
            > > > int*& a
            > > >[/color]
            > > Half of those aren't pointers.[/color]
            >
            > Half? So, say, 2.5 of those 5 variables are (or are not) pointers? Perhaps
            > this is new math? :-)[/color]

            Sure, the reference to a pointer counts as half :-)


            Comment

            • Jakob Bieling

              #7
              Re: pointer in C++

              "Yuh-Horng Shiau" <yhshiau@mail.n uu.edu.tw> wrote in message
              news:3F0D95E3.8 050308@mail.nuu .edu.tw...
              [color=blue]
              > The declaration of 'int *& a;' will get an error from the compiler.[/color]

              'int*& a;' will give you an error yes, because you are not initializing
              the reference. Just like 'int& a;' will give you an error. The following is
              valid:

              int* b = 0;
              int*& a = b;

              regards
              --
              jb

              (replace y with x if you want to reply by e-mail)


              Comment

              Working...