Explanation of Solution
Returning a pointer from the function:
If the reference pointer exists in the function, then only the function returns the pointer from the function.
Circumstance for successful return of a pointer:
The circumstances for returning pointer successful are listed below:
- When a pointer is passed as an argument in a function that is called.
- When a pointer variable is defined locally within the function there may be possibility for the contents of the variables being destructed after execution of the function. This can cause unexpected results.
- When pointer gets chunk of memory allocated dynamically.
- When a variable is allocated dynamically, the contents will be available until the compiler executes the delete operation.
Example:
// Include the necessary headers
#include <iostream>
using namespace std;
//function prototype to get alphabets
char *getalphabets();
//function prototype to display the array elements
void disparray(char[], int);
//main method
int main()
{
//variable declaration
const int SIZE = 3;
// Get the alphabets from the user
char *chkname = getalphabets();
//Display the content of array
cout << "The alphabets you entered are : ";
// Function call to display the contents of the array
disparray(chkname, SIZE);
//return t...
Want to see the full answer?
Check out a sample textbook solutionChapter 10 Solutions
STARTING OUT WITH C++ MPL
- TRUE or FALSE - In C++, a function can't return a pointer. Select one: a.FALSE b.TRUEarrow_forwardUsing C++ Programming language: Assume you want a function which expects as parameters an array of doubles and the size of the array. Write the function header that accepts these parameters but is defined in such a way that the array cannot be modified in the function. You can use your own variable names for the parameters.arrow_forwardUsing C++ Language Write a function call with arguments tensPlace, onesPlace, and userInt. Be sure to pass the first two arguments as pointers. Sample output for the given program: tensPlace = 4, onesPlace = 1 Code: #include <stdio.h> void SplitIntoTensOnes(int* tensDigit, int* onesDigit, int DecVal){ *tensDigit = (DecVal / 10) % 10; *onesDigit = DecVal % 10;} int main(void) { int tensPlace; int onesPlace; int userInt; scanf("%d", &userInt); /* Your solution goes here */ printf("tensPlace = %d, onesPlace = %d\n", tensPlace, onesPlace); return 0;}arrow_forward
- Note: Write programs in c language format use in programs (Printf & Scanf)arrow_forward3- It is not possible to change the value of the pointer. (True or False). 4- If the following lines of code have errors, correct them; otherwise, write "no errors." for (int i=2; i<5; i++) { int s=1*2; } cout << s; 5- A function cannot be called from inside another function. (True or False). 6- How to make a function return multiple values? 7- Every class member is by default. (public, private, not public nor private) 8- Create an instance of the following class and call its methods. class Exam{ int grade; public: void seta (int b) (grade=b; } int geta () {return grade; } 9- When the word const is put before the variable definition, what does that mean? 10- How to concatenate two strings in C++ language? C++arrow_forwardThis is a C programming question. Please use functions for answer. I am asking this: A program that finds the sum of the left and right diagonals of a 5 * 5 integer array (The program that finds the sums of the left and right diagonals of a 5 * 5 integer array entered on the keyboard separately and writes it on the screen)arrow_forward
- Use C language. Thanks!arrow_forwardWrite the main program to create an array of size 10. Create two integer variables called max and min. Write a function to accept this array and its size. This function must populate the array with random numbers in the range -400 to 300. Write a different function to accept this array, its size and the address of the two variables max and min. The function must find the max and min of this array and write it to the min and max using pointer notation. Print the array and the values of max and min in the main program. in C++ visual studioarrow_forwardUsing C language and using pointersarrow_forward
- Write a C++ program that determines if an integer array is a palindrome. Declare two pointers to the array. One pointer points to the starting of the array, another points to the end of the array. Using these pointers determine whether the array is a palindrome. Increment the pointer, which points to the starting of the array, and decrement the pointer which points to the end of the array. The program should ask the user to enter the array size, and then creates an integer array dynamically using the entered size. The user is then asked to fill the array, which is then checked as mentioned earlier. Remember to delete the dynamic array before exiting the program.arrow_forwardSuppose three numbers x, y, z are passed to a function. Implement this function to assign the values of y to x, z to y and x to z and return these three changed values to main. Use pointer. Note: Use C languagearrow_forwardWrite a funtion to swap the two float variables using pointers. Print the values before and after the swap. The function should take as arguments two float pointer type arguments. Initialize the float values with the two float random numbers in the range (0, 10).arrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrSystems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage LearningC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning