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 ++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.
  • Cursor in CCursor in C
    pointer in the c language is easy to learn. some tasks in 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 cursor.
  • The abort () function in CThe abort () function in C
    the abort () function stops executing the program abnormally.
  • How to use Head Pointer on a MacHow to use Head Pointer on a Mac
    head pointer is a feature in accessibility, which allows users to control the cursor on the screen with the movement of the head, through a mac camera.
  • The function atexit () in CThe function atexit () in C
    int atexit function (void (* func) (void)) calls function func when the program ends. you can register your end function (func function) wherever you like, but this function will be called at the end of the program.
  • How to fix the mouse pointer disappearing on Windows 10How to fix the mouse pointer disappearing on Windows 10
    does your mouse pointer disappear in the air after downloading the windows 10 build 2004 update?
  • The function rand () in CThe function rand () in C
    the function int rand (void) returns a random number in the range from 0 to rand_max.
  • Cursor NULL in C / C ++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.
  • How to change the mouse pointer on Windows 11 with many styles and colorsHow to change the mouse pointer on Windows 11 with many styles and colors
    windows 11 allows you to change the color and customize the mouse pointer to your liking. the following will be instructions on how to change the mouse pointer on windows 11...
  • The exit () function in CThe exit () function in C
    the void exit (int status) function immediately terminates the calling process. any file opened by the process is closed and any child process is inherited by the initial process and the parent process is sent a sigchild signal.