lead.smart34's Avatar, Join Date: Feb 2008
Contributor
good info
crazytolearn57's Avatar, Join Date: Feb 2008
Go4Expert Member
good one
aisha.ansari84's Avatar, Join Date: Feb 2008
Contributor
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's Avatar, Join Date: Jul 2007
Go4Expert Member
Quote:
Originally Posted by aisha.ansari84
i agree to it
Thanks to All.
pavankumar.thati's Avatar, Join Date: Jul 2008
Newbie Member
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...........

Last edited by shabbir; 10Jul2008 at 09:32.. Reason: Code block
Darrelljones's Avatar, Join Date: Jan 2011
Go4Expert Member
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's Avatar, Join Date: Jan 2011
Ambitious contributor
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's Avatar
Banned
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's Avatar
Banned
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's Avatar, Join Date: Mar 2011
Go4Expert Member
Really nice tutorial, thanks for sharing!!..