Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/articles/cpp-tutorials/)
-   -   Difference between Pointers and Reference in C++ (http://www.go4expert.com/articles/difference-pointers-reference-cpp-t5492/)

lead.smart34 26Feb2008 18:17

Re: Difference between Pointers and Reference in C++
 
good info

crazytolearn57 26Feb2008 18:39

Re: Difference between Pointers and Reference in C++
 
good one

aisha.ansari84 5Mar2008 18:36

Re: Difference between Pointers and Reference in C++
 
Quote:

Originally Posted by DaWei
That is incorrect, zaka. A reference is a second name (alias) for an object. You can't name something that doesn't exist, therefore you have to specify the object being aliased when you declare the reference. When a reference is passed as an argument, a copy of the objects address is passed. This is one fewer levels of indirection than when a pointer to the object is passed.

i agree to it

Shishir191 7May2008 17:00

Re: Difference between Pointers and Reference in C++
 
Quote:

Originally Posted by aisha.ansari84
i agree to it

Thanks to All.

pavankumar.thati 10Jul2008 06:53

Difference between Pointers and Reference in C++
 
there is a class Date and a function
the function is non static

Date& and *this is same in representation and they are explain plzzzzzzz.........

Code:

class Date
{
              Date& add_year(int);
              Date& add_month(int);
              Date& add_day(int);
}
int main()
{
              Date a();
            a.add_year(1).add_month(2).add_day(2);
                return 0;
}
Date& Date::add_year(int n)
{
                  if(d==29&&m==2&&!leapyear(y+n)
                  {
                              d=1;
                              m=3;
                  }
                    y+=n;
                    return  *this;
}
Date& Date::add_month(int n)
{
                return *this;
}
Date& Date::add_day(int n)
{
              return *this;
}

Is this a valid one if then give me some explanation with examples.
plzzzzzzzzzzzzz...........

Darrelljones 28Jan2011 00:27

Re: Difference between Pointers and Reference in C++
 
A pointer can be re-assigned any number of times while a reference can not be reassigned after initialization.
A pointer can point to NULL while reference can never point to NULL
You can't take the address of a reference like you can with pointers
There's no "reference arithmetics" (but you can take the address of an object pointed by a reference and do pointer arithmetics on it as in &obj + 5).

As a general rule,

Use references in function parameters and return types to define attractive interfaces.
Use pointers to implement algorithms and data structures.

poornaMoksha 29Jan2011 19:05

Re: Difference between Pointers and Reference in C++
 
Shishir pointed out in the very first post of this discussion that " Its not necessary to initialize the pointer at the time of declaration". I think its not mandatory but still we should always make sure that a pointer is initialized to NULL whenever it is declared because :

Code:

int main()
{
    char *ptr;
    int retval = 0;
    retval = someFunc();
    if(retval)
        ptr = "I got some address";
    someOtherFunc(ptr);
}

void someOtherFunc(char *ptr)
{
    if(ptr)
    {
        // use ptr
    }
}

Now in the above code snippet, suppose the value of 'retval' is 0 which ensures 'ptr' does not get address of string "I got some address" and inside the function 'someOtherFunc' the condition 'if(ptr)' may well be true as ptr may be holding some garbage address which is non-null and hence when we use ptr we may well observe a crash as through 'ptr' the program would try to access the garbage address

kumarmannu 3Feb2011 14:48

Re: Difference between Pointers and Reference in C++
 
1. A pointer can be re-assigned any number of times while a reference can not be reassigned after initialization.
2. A pointer can point to NULL while reference can never point to NULL
3. You can't take the address of a reference like you can with pointers
4. There's no "reference arithmetics" (but you can take the address of an object pointed by a reference and do pointer arithmetics on it as in &obj + 5).

1) A pointer can be re-assigned:

int x = 5;
int y = 6;
int *p;
p = &x;
p = &y;
*p = 10;
assert(x == 5);
assert(y == 10);

A reference cannot, and must be assigned at initialization:

int x = 5;
int y = 6;
int &r = x;
A pointer needs to be dereferenced with * to access the memory location it points to, whereas a reference can be used directly. A pointer to a class/struct uses -> to access it's members whereas a reference uses a .

A pointer is a variable that holds a memory address. Regardless of how a reference is implemented, a reference has the same memory address the item it references.

References cannot be stuffed into an array, whereas pointers can be (Mentioned by user @litb)

kumarpyasa 1Mar2011 12:40

Re: Difference between Pointers and Reference in C++
 
Hello dear
Pointers and references are the different
1. pointers use the * and -> operators and references use .
2. A pointer can be re-assigned any number of times while a reference can not be reassigned after
initialization.
3.A pointer can point to NULL while reference can never point to NULL
4. You can't take the address of a reference like you can with pointers
5. There's no "reference arithmetics" (but you can take the address of an object pointed by a
reference and do pointer arithmetics on it as in &obj + 5).

alexsmth114 8Mar2011 12:10

Re: Difference between Pointers and Reference in C++
 
Really nice tutorial, thanks for sharing!!..


All times are GMT +5.5. The time now is 21:51.