Table of Contents
Call the Function by Pointer in C ++ is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.
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 helps 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 example below:
#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:
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 ++
Frequently Asked Questions
What is 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.
Why is Call the Function by Pointer in C ++ important?
A clear understanding of Call the Function by Pointer in C ++ helps you make informed decisions, avoid common mistakes, and use the relevant tools or techniques more effectively.
How should beginners approach Call the Function by Pointer in C ++?
Start with the fundamental concepts, follow the examples step by step, and test each change in a safe environment before applying it to important systems or data.
Was this article helpful?
Your feedback helps us improve.
Reader Comments 0
Sign in with email or Google to join the discussion.