pointer variable vs reference variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sulekhasweety@gmail.com

    pointer variable vs reference variables

    dear all,


    can anyone explain the differences between pointer variable and
    reference variables ?
  • santosh

    #2
    Re: pointer variable vs reference variables

    sulekhasweety@g mail.com wrote:
    dear all,
    >
    >
    can anyone explain the differences between pointer variable and
    reference variables ?
    You'll have to ask in a C++ group (comp.lang.c++) or in a group
    appropriate to whatever language you are using (presumably it contains
    both pointers and references, hence your question). C has no
    references. If you want language independent responses, then
    comp.programmin g might be a better place to post this question.

    Comment

    • =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?=

      #3
      Re: pointer variable vs reference variables

      On Jun 7, 8:25 am, sulekhaswe...@g mail.com wrote:
      dear all,
      >
      can anyone explain the differences between pointer variable and
      reference variables ?

      References don't exist in C, they're a C++ thing. A reference is
      simply a pointer with the following features:
      * You don't have to dereference it, it gets dereferenced
      automagically
      * You can't change what it points to

      So the following two are equivalent:

      void Func(int const *p) { *p = 5; }

      void Func(int &i) { p = 5; }

      If you want references in C, then you can play around with macroes:

      void Func(int const *p)
      {
      # define i (*p)

      i = 5;
      }

      Comment

      • =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?=

        #4
        Re: pointer variable vs reference variables

            void Func(int &i) { p = 5; }

        i = 5;

        Comment

        • =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?=

          #5
          Re: pointer variable vs reference variables

          On Jun 7, 9:05 am, Tomás Ó hÉilidhe <t...@lavabit.c omwrote:
              void Func(int const *p) { *p = 5; }

          int *const p

              void Func(int const *p)

          int *const p

          I should go back to bed.

          Comment

          • Malcolm McLean

            #6
            Re: pointer variable vs reference variables


            <sulekhasweety@ gmail.comwrote in message news:
            dear all,
            >
            >
            can anyone explain the differences between pointer variable and
            reference variables ?
            >
            References exist in Java and C++, and are basically syntactic sugar for
            pointers. They point only to one object, and may never be null. However the
            variable is passed as an address, ie a pointer.

            --
            Free games and programming goodies.


            Comment

            • nembo kid

              #7
              Re: pointer variable vs reference variables

              sulekhasweety@g mail.com ha scritto:
              can anyone explain the differences between pointer variable and
              reference variables ?
              It is useless since "reference variables" is not C.

              Comment

              • rahul

                #8
                Re: pointer variable vs reference variables

                On Jun 7, 2:19 pm, "Malcolm McLean" <regniz...@btin ternet.comwrote :
                References exist in Java and C++, and are basically syntactic sugar for
                pointers. They point only to one object, and may never be null.
                A bit off the topic, but references can be null in Java. References in
                Java work
                like constant pointers in C.

                Comment

                • Eric Sosman

                  #9
                  [OT] Re: pointer variable vs reference variables

                  Malcolm McLean wrote:
                  >
                  <sulekhasweety@ gmail.comwrote in message news:
                  >dear all,
                  >>
                  >>
                  >can anyone explain the differences between pointer variable and
                  >reference variables ?
                  >>
                  References exist in Java and C++, and are basically syntactic sugar for
                  pointers. They point only to one object, and may never be null. However
                  the variable is passed as an address, ie a pointer.
                  <off-topic>

                  Dunno about C++, but what you say about Java is wrong on
                  all three points (or at least on two and a half points, allowing
                  some wiggle room in interpreting "they" in the second point):

                  1) A Java "reference" *is* what C calls a "pointer," not a
                  pointer hiding behind a sugar-coated facade. (Java gives
                  the programmer little freedom to manipulate its pointers,
                  but they're indisputably pointers. Note what's thrown for
                  misuse of a null reference: NullPointerExce ption.)

                  2) A Java reference value refers to only one object, just as
                  a C pointer value refers to only one object. A Java
                  reference variable, just like a C pointer variable, can
                  refer to different objects at different times.

                  3) A Java reference value or variable can be null.

                  </off-topic>

                  --
                  Eric Sosman
                  esosman@ieee-dot-org.invalid

                  Comment

                  • Malcolm McLean

                    #10
                    Re: [OT] Re: pointer variable vs reference variables

                    "Eric Sosman" <esosman@ieee-dot-org.invalidwrot e in message
                    Malcolm McLean wrote:
                    >>
                    ><sulekhasweety @gmail.comwrote in message news:
                    >>dear all,
                    >>>
                    >>>
                    >>can anyone explain the differences between pointer variable and
                    >>reference variables ?
                    >>>
                    >References exist in Java and C++, and are basically syntactic sugar for
                    >pointers. They point only to one object, and may never be null. However
                    >the variable is passed as an address, ie a pointer.
                    >
                    <off-topic>
                    >
                    Dunno about C++, but what you say about Java is wrong on
                    all three points (or at least on two and a half points, allowing
                    some wiggle room in interpreting "they" in the second point):
                    >
                    1) A Java "reference" *is* what C calls a "pointer," not a
                    pointer hiding behind a sugar-coated facade. (Java gives
                    the programmer little freedom to manipulate its pointers,
                    but they're indisputably pointers. Note what's thrown for
                    misuse of a null reference: NullPointerExce ption.)
                    >
                    2) A Java reference value refers to only one object, just as
                    a C pointer value refers to only one object. A Java
                    reference variable, just like a C pointer variable, can
                    refer to different objects at different times.
                    >
                    3) A Java reference value or variable can be null.
                    >
                    </off-topic>
                    >
                    Java references can be null, fair point. The rest of this post is singularly
                    useless and unhelpful. Do you appreciate why?

                    --
                    Free games and programming goodies.



                    Comment

                    Working...