Call the function by pointer in C ++

The method of calling a function by pointer in C ++ passes parameters to a function, copying the addresses of a parameter into the official parameter. Inside the function, this address is used to access the actual parameter used in the function call. That is, changes made to the official parameter affect the passed parameter.

To pass values ​​by pointers, parameter pointers are passed to functions like any other value. Therefore, you need to declare the function parameters as a pointer type as in the following traodoi () function, which exchanges the values ​​of two integer variables pointed by its parameters.

 // phan dinh nghia ham de trao doi cac gia tri. void traodoi ( int * x , int * y ) { int temp ; temp = * x ; /* luu giu gia tri tai dia chi x */ * x = * y ; /* dat y vao trong x */ * y = temp ; /* dat x vao trong y */ return ; } 

To check the details of the cursor in C ++, you access the chapter: Cursor in C ++.

Now, call the traodoi () function by passing the values ​​by the pointer as in the following example:

 #include using namespace std ; // Phan khai bao ham void traodoi ( int * x , int * y ); int main () { // Khai bao bien cuc bo: int a = 100 ; int b = 200 ; cout << "Truoc khi trao doi, gia tri cua a la: " << a << endl ; cout << "Truoc khi trao doi, gia tri cua b la: " << b << endl ; /* goi ham traodoi de trao doi cac gia tri cua cac bien. * &a chi rang con tro dang tro toi a (dia chi cua bien a) va * &b chi rang con tro dang tro toi b (dia chi cua bien b). */ traodoi (& a , & b ); cout << "Sau khi trao doi, gia tri cua a la: " << a << endl ; cout << "Sau khi trao doi, gia tri cua b la: :" << b << endl ; return 0 ; } 

You put the above function definition at the end of this code, then compile and run the above C ++ program will produce the following result:

Call the function by pointer in C ++ Picture 1 Call the function by pointer in C ++ Picture 2

You should also follow the chapter: Call the function by value in C ++ to compare this difference.

According to Tutorialspoint

Previous lesson: Calling functions by value in C ++

Next lesson: Call the function by reference in C ++

5 ★ | 1 Vote

May be interested

  • Call the function by reference in C ++Photo of Call the function by reference in C ++
    the method of calling a function by reference of parameters passed to a function copies the address of a parameter into the official parameter. within that function, the address used to access the parameter is actually used in calling the function. this means that changes are made with parameters that affect the passed parameters.
  • Number in C ++Photo of Number in C ++
    normally, when we work with number (numeric types), we use the original data types such as int, short, long, float and double, ... digital data types, values possible and their range of values, discussed in the data type chapter in c ++.
  • Array (Array) in C / C ++Photo of Array (Array) in C / C ++
    c / c ++ programming language provides data structures called arrays, stored in a set of data of the same type with fixed length. an array is used to store data sets, but it is useful if you think of an array of variables with the same type.
  • String (String) in C / C ++Photo of String (String) in C / C ++
    this form of string originates from c language and continues to be supported in c / c ++. the string in the c programming language is essentially a one-dimensional array of characters that ends with a null character ''.
  • Cursor in C / C ++Photo of Cursor in C / C ++
    pointer - pointer in c / c ++ language is easy to learn and interesting. some tasks in the c / c ++ language are made easier by pointers, and other tasks become more flexible, such as in memory allocation, which cannot be performed without using a pointer.
  • Cursor NULL in C / C ++Photo of Cursor NULL in C / C ++
    it is always a good practice to assign a null pointer to a pointer variable in case you don't know the exact address to be assigned. this is done at the time of variable declaration. a pointer that is assigned null is called a null pointer.