Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. what is difference betwn pointer and referece

what is difference betwn pointer and referece

Scheduled Pinned Locked Moved C / C++ / MFC
question
12 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rajesh match
    wrote on last edited by
    #1

    basic things of reference and pointer but can anybody explain me how they operate differently thax

    M J 2 Replies Last reply
    0
    • R Rajesh match

      basic things of reference and pointer but can anybody explain me how they operate differently thax

      M Offline
      M Offline
      melwyn
      wrote on last edited by
      #2

      reference is nothing but a constant pointer.

      S 1 Reply Last reply
      0
      • M melwyn

        reference is nothing but a constant pointer.

        S Offline
        S Offline
        souldog
        wrote on last edited by
        #3

        Really? try compilling these const int* p = NULL; and int& l = NULL;

        M 1 Reply Last reply
        0
        • S souldog

          Really? try compilling these const int* p = NULL; and int& l = NULL;

          M Offline
          M Offline
          melwyn
          wrote on last edited by
          #4

          ok correction :)...for most purposes a reference can be looked at as a constant pointer.

          R 1 Reply Last reply
          0
          • M melwyn

            ok correction :)...for most purposes a reference can be looked at as a constant pointer.

            R Offline
            R Offline
            Rajesh match
            wrote on last edited by
            #5

            i think reference is address of the object.. unlike pointer, u cannot delete reference pointer is links to memory but reference sit on the top of the object.. reference never be the pointer , reference just another name of the object.

            M 1 Reply Last reply
            0
            • R Rajesh match

              i think reference is address of the object.. unlike pointer, u cannot delete reference pointer is links to memory but reference sit on the top of the object.. reference never be the pointer , reference just another name of the object.

              M Offline
              M Offline
              melwyn
              wrote on last edited by
              #6

              reference is internally implemented as a constant pointer with some conditions.

              R 1 Reply Last reply
              0
              • M melwyn

                reference is internally implemented as a constant pointer with some conditions.

                R Offline
                R Offline
                Rajesh match
                wrote on last edited by
                #7

                what's that conditions

                M 1 Reply Last reply
                0
                • R Rajesh match

                  basic things of reference and pointer but can anybody explain me how they operate differently thax

                  J Offline
                  J Offline
                  Joaquin M Lopez Munoz
                  wrote on last edited by
                  #8

                  References may be implemented as pointers (though not constant ones as someone has said), but this is unimportant from the user's point of view. I guess you already know what a pointer is, so let's focus on references. You can think of a reference to an object as an alias to that object, i.e. an alternate name for the object. So:

                  int a=0;
                  int& b=a; // b is a new name for a
                  b=1;
                  cout<<a; // outputs 1

                  In this example a and b refer to the very same object. There is a common use of references as convenient arguments to function. Consider:

                  void f(int i)
                  {
                  i=1;
                  }

                  int a=0;
                  f(a);
                  cout<<a; // outputs 0!

                  f is not doing what you intended because you are passing the value of a, not the variable itself (in terms of implementation, you're passing an int constructed from a.) What f needs is the "name" of the object:

                  void f(int& i)
                  {
                  i=1;
                  }

                  int a=0;
                  f(a);
                  cout<<a; // outputs 1

                  Get the idea? Of course you could have gotten the same effect using pointers, in this case at least it is a matter of convenience and style which solution you use. References are somewhat hard to grasp, keep on reading good C++ stuff and the concept will progressively build in your mind. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                  S 1 Reply Last reply
                  0
                  • R Rajesh match

                    what's that conditions

                    M Offline
                    M Offline
                    melwyn
                    wrote on last edited by
                    #9

                    some of them have already been mentioned in this thread. One more is * reference has to be initialized when defined, but pointer need not be e.g. const int* p; int& l; //error here

                    1 Reply Last reply
                    0
                    • J Joaquin M Lopez Munoz

                      References may be implemented as pointers (though not constant ones as someone has said), but this is unimportant from the user's point of view. I guess you already know what a pointer is, so let's focus on references. You can think of a reference to an object as an alias to that object, i.e. an alternate name for the object. So:

                      int a=0;
                      int& b=a; // b is a new name for a
                      b=1;
                      cout<<a; // outputs 1

                      In this example a and b refer to the very same object. There is a common use of references as convenient arguments to function. Consider:

                      void f(int i)
                      {
                      i=1;
                      }

                      int a=0;
                      f(a);
                      cout<<a; // outputs 0!

                      f is not doing what you intended because you are passing the value of a, not the variable itself (in terms of implementation, you're passing an int constructed from a.) What f needs is the "name" of the object:

                      void f(int& i)
                      {
                      i=1;
                      }

                      int a=0;
                      f(a);
                      cout<<a; // outputs 1

                      Get the idea? Of course you could have gotten the same effect using pointers, in this case at least it is a matter of convenience and style which solution you use. References are somewhat hard to grasp, keep on reading good C++ stuff and the concept will progressively build in your mind. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                      S Offline
                      S Offline
                      souldog
                      wrote on last edited by
                      #10

                      I would say it is not a matter of style and convenience. In most circumstances only pass pointers into functions if you need the possibility that the object is not valid, i.e. null pointer. It is a matter of the compiler catching errors or having them show up at runtime.

                      J 1 Reply Last reply
                      0
                      • S souldog

                        I would say it is not a matter of style and convenience. In most circumstances only pass pointers into functions if you need the possibility that the object is not valid, i.e. null pointer. It is a matter of the compiler catching errors or having them show up at runtime.

                        J Offline
                        J Offline
                        Joaquin M Lopez Munoz
                        wrote on last edited by
                        #11

                        It is a matter of the compiler catching errors or having them show up at runtime. I don't think substituting references for pointers in function declarations catches any error at runtime. For instance, consider a program like this

                        void foo(int *i);

                        ...

                        // invocations of f have one of these two forms

                        f(something);
                        f(&something);

                        If I change the definition of foo to allegedly make it more robust, the whole program has to be changed as

                        void foo(int& i);

                        ...

                        f(*something);
                        f(something);

                        Any program compiling with the first definition of foo will compile just the same with the second definition (after modifying the invocations to foo.) So, you won't catch more errors at compile time. References can help prevent problems at compile time, but not when used as function arguments --rather, when replacing pointer variables declarations with reference variables declarations, as in

                        // version 1
                        int * p;

                        // version 2
                        int& p=... // it is mandatory that p be initialized, which may help preserve
                        // the sanity of the program

                        Anyway replacing pointers with references is not always feasible, if the concept of null object is valid, or if the pointer must be rebound (references cannot.) Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                        S 1 Reply Last reply
                        0
                        • J Joaquin M Lopez Munoz

                          It is a matter of the compiler catching errors or having them show up at runtime. I don't think substituting references for pointers in function declarations catches any error at runtime. For instance, consider a program like this

                          void foo(int *i);

                          ...

                          // invocations of f have one of these two forms

                          f(something);
                          f(&something);

                          If I change the definition of foo to allegedly make it more robust, the whole program has to be changed as

                          void foo(int& i);

                          ...

                          f(*something);
                          f(something);

                          Any program compiling with the first definition of foo will compile just the same with the second definition (after modifying the invocations to foo.) So, you won't catch more errors at compile time. References can help prevent problems at compile time, but not when used as function arguments --rather, when replacing pointer variables declarations with reference variables declarations, as in

                          // version 1
                          int * p;

                          // version 2
                          int& p=... // it is mandatory that p be initialized, which may help preserve
                          // the sanity of the program

                          Anyway replacing pointers with references is not always feasible, if the concept of null object is valid, or if the pointer must be rebound (references cannot.) Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                          S Offline
                          S Offline
                          souldog
                          wrote on last edited by
                          #12

                          Well what I meant was void FOO(int* p){*p+=1;} int* i = NULL; FOO(i) If you use references then this sort of problem just can not occur. So it is not that an error will be caught at compile time, there can be no error if you use references. Anyway replacing pointers with references is not always feasible, if the concept of null object is valid, or if the pointer must be rebound (references cannot.) I agree and that is what I said.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups