In any language, if you pass a variable's reference, then you can change the value of the referenced variable with the code that obtained the reference.
If you pass the value of a variable, the code which obtains it cannot change the value of the original variable.
For example:
int x = 100;
void myfunction(%26amp;int z){
//some code
z = 30
}
would make x = 30!
If you do this:
void myfunction(int z) {
//some code
z = 30
}
x still equals 100.
What is the difference between copying a value and copying a reference in c language?
Copying a value is simply assign the variable to another one
eg:int x = 10;
int y = x /* copy of x is y . Now y= 10 */
Copying a reference means copy the address location of the variable
eg: int x = 10;
int *y = %26amp;x /* here y is assigned with a place holder (addtress location) only
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment