Sunday, July 12, 2009

What is differnce between pointer and referance in c?

A pointer points as a reference, and a reference references a pointer, except in the case of a reference pointer which points to a reference to a reference of a pointer.

What is differnce between pointer and referance in c?
same
Reply:pointers can be called as naked reference.





when you say its reference means you are actually operating on that variable. it is a kind of alias





where as in case of pointer, if pointer is pointing to something, and now you change that pointer to point something else, your original variable will still there unchanged.





but in case of reference, once you make a reference you can not do a un-reference. Cannot remove a reference once it referred to something.





references are created while initialization only, where as you can use pointer at any moment.





i hope now its clear what is reference and what is pointer.





int x=10;


int *p=0;


int %26amp;y=x;





now p = %26amp;x;





if you do p=NULL, value of x will not change.


if you do y=0;, actually value of x will become 0
Reply:You can consider them to be the same.
Reply:A pointer is a variable that stores the memory address of another object. A reference IS the memory address of an object. You can think of a pointer as a variable that represents a reference.





For instance:


{


int myNumber = 14;


int *number = %26amp;myNumber;


}





In this example, we create two variables. "myNumber" is an integer variables that we set equal 14. "number" is a pointer to an integer variable, that we set equal to "%26amp;myNumber". "%26amp;myNumber" is the memory reference to the variable "myNumber".





The major difference in using pointers vs. references is that a pointer can store a null value. A reference must identify a valid object. Also, the value of a pointer can change. We can make it point to a different reference later on in the program. A variable's reference is constant and cannot be changed.
Reply:A reference is the actual memory address of the variable. Passing things into functions by reference means you are actually sending the memory address, and therefore make that variable modifiable inside the function (instead of just passing it in by value, which creates a copy of that variable's value for its own use)





A pointer has its own memory reference like a normal variable, but its actual value is the reference to another property's memory address, so that when you look at a pointer's value, it is actually the memory address of the property its pointing to.
Reply:allmost about the same thing
Reply:A pointer contains some address that refers to the location of an object. Hence, a pointer points to an object. A reference is an alias to an object. In that sense, a reference IS an object itself. References are not pointers. Let me repeat. References are not pointers. You do not dereference them. You do not reseat them. You do not use pointer logic with them. They are objects themselves.





Consequently, pointers and references are NOT the same. The compiler may resolve a reference to a pointer or the object itself. That is not your concern.





(trueextremeicon) %26gt; A reference is the actual memory address of the variable.





No. It's not. It's the object itself, for all purposes. You do not dereference a reference.





%26gt; Passing things into functions by reference means you are actually sending the memory address





No. It means that you are referring to the object itself, not a temporary copy. The compiler may resolve it by passing a memory address. You do not care.





(marbledog) %26gt; A reference IS the memory address of an object





No. It is not. A pointer contains some address that on dereferencing gets you the object. A reference is the object itself, for all purposes. You do NOT dereference a reference.





See http://www.parashift.com/c++-faq-lite/re... .


No comments:

Post a Comment