The difference between values of the data type and data type references programming language

1. The value of the data type stored on the stack, the type of reference data values ​​stored in the stack, the stack is stored in its reference.

For example :( to c ++, for example), other similar language

Basic data types:

// allocates memory stored on the stack i, the variable i has an address that is to say, the value which is stored in 10 
int i = 10 ;

Reference data types:

// open a memory array is stored in the stack will be {1,2,3,4}
 // in the stack will open a memory to store a variable arr, arr value which is stored in a heap {1,2,3,4} the address should be noted that arr itself has its own address, but the address is stored arr itself 
int arr [] = { . 1 , 2 , . 3 , . 4 };

2. The value of the data type is passed in the parameter value passed, the value is passed to the parameter, the parameter in the function of the change does not affect the value of the argument; reference data type parameter passing is passed by reference, i.e. value passed is the address, in the function and parameter changes will affect the value of the argument. Of course, the data may be the value of the address type parameter passed as an argument, and this is quite a reference.

For example :( to c ++, for example), other similar language

Value is passed:

#include <the iostream>
 the using  namespace STD; 

void the swap ( int num1, int num2) { 
    COUT << " value before switching the num1: " << num1 << endl; 
    COUT << " value before switching the num2: " << << num2 endl;
     int tmp = num1; 
    num1 = num2; 
    num2 = tmp; 
    COUT << " num1 values after an exchange: " << num1 << endl; 
    COUT <<" Num2's value after the exchange:" << num2 << endl; 
} 

int main () 
{ 
    int A = . 1 ;
     int B = 2 ; 
    COUT << " A previous value is passed in argument: " << A << endl; 
    COUT << " not previously passed the argument value of b: " << b << endl; 
    the swap (a, b); 
    COUT << " a value after incoming arguments: " << a << endl; 
    COUT << " the argument value b after passing: " << b << endl;
    system("pause");
    return 0;
}

Output:

Passed by reference (using the value passed address):

 

#include <the iostream>
 the using  namespace STD; 

void the swap ( int * num1, int * num2) { 
    COUT << " exchange of values before num1: " << num1 * << endl; 
    COUT << " value before switching num2 of: " << num2 * << endl;
     int tmp = * num1;
     * * = num1 num2;
     num2 = * tmp; 
    COUT << " value after switching num1: " << num1 * << endl; 
    COUT <<" Num2 value after the exchange: "* Num2 << << endl; 
} 


int main () 
{ 
    int a = . 1 ;
     int B = 2 ;
     int * P1 = & a ;
     int * P2 = & B; 
    COUT << " argument of a value not previously passed : " << a << ; endl 
    COUT << " : not previously passed the argument value of b " << b << endl; 
    ; the swap (P1, P2) 
    COUT << " value after the argument passed a : " << A << endl;
    cout << " b values after the arguments passed:" << b << endl;
    system("pause");
    return 0;
}

Output :

Passed by reference (reference data type itself, in c ++, the array is a reference data type):

void Transform ( int ARR []) { 
    ARR [ 0 ] = . 9 ; 
} 

int main () {
     int ARR [] = { . 1 , 2 , . 3 , . 4 , . 5 };
         // by the function value is changed to a first array is. 9   
    Transform (ARR); 
    COUT << ARR [ 0 ] << endl; 
    System ( " PAUSE " );
     return  0 ; 
}

Output:

Guess you like

Origin www.cnblogs.com/xiximayou/p/12080317.html