Pass cursor to function in C ++

C ++ allows you to pass a pointer to a function. To do this, you simply need to declare the function parameter as in a pointer type.

In the simple example below, we pass an unsigned long pointer to a function and change the value inside the function, which reflects back while calling the function:

 #include #include using namespace std ; void laySoGiay ( unsigned long * par ); int main () { unsigned long sogiay ; laySoGiay ( & sogiay ); // in gia tri cout << "So giay la: " << sogiay << endl ; return 0 ; } void laySoGiay ( unsigned long * par ) { // Lay so giay hien tai * par = time ( NULL ); return ; } 

Running the above C ++ program will produce the following results:

Pass cursor to function in C ++ Picture 1

The function, which can accept a pointer, can accept an array as follows:

 #include using namespace std ; // khai bao ham: double giaTriTB ( int * arr , int size ); int main () { // mot mang so nguyen co 5 phan tu. int doanhSoBH [ 5 ] = { 122 , 35 , 27 , 235 , 60 }; double trungbinh ; // truyen con tro toi mang duoi dang mot tham so. trungbinh = giaTriTB ( doanhSoBH , 5 ) ; // hien thi ket qua cout << "Gia tri trung binh la: " << trungbinh << endl ; return 0 ; } double giaTriTB ( int * arr , int size ) { int i , sum = 0 ; double trungbinh ; for ( i = 0 ; i < size ; ++ i ) { sum += arr [ i ]; } trungbinh = double ( sum ) / size ; return trungbinh ; } 

When the above C ++ program is compiled and executed, it gives the following result:

 Gia tri trung binh la : 95.8 

According to Tutorialspoint

Previous article: Pointers to pointers in C ++

Next article: Returns the cursor from the function in C ++

5 ★ | 1 Vote

May be interested

  • Returns the pointer from the function in C ++Photo of Returns the pointer from the function in C ++
    as we have seen how c ++ allows to return an array from a function, similarly, c ++ allows you to return a pointer from a function.
  • C ++ exercises have solutions (sample code) for variables, data types, and operatorsPhoto of C ++ exercises have solutions (sample code) for variables, data types, and operators
    in the previous c ++ exercises, tipsmake.com introduced, you were acquainted with many types of lessons, from simple to complex. in this c ++ exercise, we will become familiar with c ++ exercises on variables and data types in c ++.
  • C ++ exercises about IF ELSEPhoto of C ++ exercises about IF ELSE
    in the previous c ++ exercise, we introduced you to read c ++ exercises about variables and data types. this time, there are 8 c ++ exercises about if else for you to practice and proficiently use if, else in c ++, please watch it.
  • Comment in C / C ++Photo of Comment in C / C ++
    program comments are interpretations, which you can include in c / c ++ code, and make it easier for anyone to read the source code. all programming languages ​​allow certain comments.
  • Data type in C / C ++Photo of Data type in C / C ++
    data type in c / c ++ while working with any programming language, you need to use various types of variables to store information. variables, nothing but memory locations are reserved for saving values. that is, when you create a variable, you reserved some space in memory for that variable.
  • Variable type in C / C ++Photo of Variable type in C / C ++
    a variable provides named storage so that we can manipulate. each variable in c / c ++ has a specific type, which determines: the size and memory layout of the variable; the range of values ​​can be stored inside that memory; and the set of activities can be applied to that variable.