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. To do this, you must declare a function that returns a pointer as follows:
int * tenHam () { . . . }
The second thing to keep in mind is that it is not a good idea to return the address of a local variable to the periphery of a function, so you will have to define a local variable as a static variable.
Now suppose the following function will generate 10 random numbers and return them using an array name that represents a pointer, for example, the first address of the first array element.
#include #include #include using namespace std ; // phan dinh nghia ham de tao va tra ve cac so ngau nhien. int * soNgauNhien ( ) { static int r [ 10 ]; srand ( ( unsigned ) time ( NULL ) ); for ( int i = 0 ; i < 10 ; ++ i ) { r [ i ] = rand (); cout << r [ i ] << endl ; } return r ; } // ham main de goi phan dinh nghia ham tren. int main () { // mot con tro tro toi mot so nguyen. int * p ; p = soNgauNhien (); for ( int i = 0 ; i < 10 ; i ++ ) { cout << "Gia tri cua *(p + " << i << ") : " ; cout << *( p + i ) << endl ; } return 0 ; }
Running the above C ++ program will produce the following results:
According to Tutorialspoint
Previous article: Pass cursor to function in C ++
Next article: Reference in C ++
You should read it
May be interested
- The function getenv () in Cchar * getenv (const char * name) function searches the environment string pointed to by the name parameter and returns the value associated with that string.
- 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.
- WEIBULL function - The function returns the Weibull distribution in Excelthe function performs the calculation and returns the weibull distribution. based on this distribution to analyze reliability in theory such as calculating the average life of the device or used in the field of meteorology, hydrology and weather forecast.
- FISHER function - The function returns the Fissher transformation at x in Excelfisher function: the function returns the fissher transformation at x. this transformation creates a normal distribution function, using this function to test a hypothesis based on a correlation coefficient. syntax: fisher (x)
- POISSON function - The function returns the Poisson distribution in Excelin predicting the number of events that occur within a specific time, one cannot help but use the poisson function. the following article details how to use the poisson function, the function returns the poisson distribution.
- FTEST function - The function returns the result of an F-Test in Excelthe following article details how to use ftest - the function returns the result of an f-test. in statistical probability, the determination of the correctness and the deviation between two data samples is a regular and extremely important job.
- QUARTILE function - The function returns the quartile of a dataset in Excelthe quartile is an indispensable value in the field of surveying and sales, it helps you evaluate certain criteria in a set. the following article details how to use quartile function, the function returns the quartile of a dataset.
- GAMMA function - The function returns the gamma function value in Excelgamma: the function returns the gamma function. support functions from excel 2013 onwards. syntax: gamma (number)
- WEEKDAY function - The function returns the weekday of a corresponding date in Excelweekday function: the function returns the weekday of a corresponding day. the return value is an integer between 1 (sunday) and 7 (saturday). syntax: weekday (serial_number, [return_type])
- F.TEST function - The function returns the result of the F - test in Excelf.test: the function returns the result of the f - test, the probability of two ends where the variance of 2 arrays is not significant. support functions from excel 2010 version. syntax: f.test (array1, array2)