say
int a[];
int &p=a;
int *q=a;

what is the difference b/w p and q ??
Is it like q can retrieve the value of the address which it is pointing to where p cant do that ... or is there something else also.....

when you pass a variable by reference, any changes you make to the variable will be done as well to the passed variable.

A pointer does the same only it passes the address of the variable and from there you can change it by De-referencing it '*'.

references make it easier if you don't want to deal with anything other than settings a var's value and such.

This is the way I understand it:

A reference variable such as "p" becomes equivalent to, or a synonym for, "a" and you can use the variable names interchangeably without any special operations (although it's not really recommended for maintainability reasons). If you change "p" you change "a" and vice versa.

Your syntax isn't correct, but a pointer variable such as "q" is not equivalent to "a". It stores the address of "a" rather than the value of "a". As a result, you need a special operator to access the value that is "pointed to" by the variable "q". If you change "q", it stores a different address and no longer points to "a" and the variable "a" remains unchanged.

ok now i understand that reference is like another name for a variable......
then wat could be said "when function return type is a reference" ??
wat does that mean and that time wat could be said about the scope of the reference variable??

Your syntax isn't correct, but....

I don't see anything wrong with the syntax he posted. int* q = a; is perfectly valid as long as a is an array of integers.

[edit]Oh, I think you meant int &p=a; , which is illegal.

hey ppl !!! any comment about the function return type being a Reference variable ????????

functions return a reference so that there is less overhead in the function return. whatever value you store the return into will hold that value and it doesn't matter that the variable that was returned goes out of scope.

// non reference return
int foo()
{
       int a = 20;
       return a;
}
// reference return
int & foo()
{
       int a = 20;
       return a;
}

as you can see there is no difference between the code except how the return value is returned. it will still get assigned to the variable you have receiving the return.

>>it doesn't matter that the variable that was returned goes out of scope.
Of course it does indeed matter. Returning a reference as in your second code snippet is very similar to returning a pointer like this:

int* foo()
{
   int a = 10;
   return &a;
}

Both the reference you posted and the pointer I posted above are wrong and will most likely cause problems for the calling function.

then how would you return a value by reference if it is going out of scope or should you return by value if the variable is going out of scope?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.