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
- C Program Functions using Pointers Create a function modify that accepts an integer and divides the integer by 2 if it is even. If the integer is odd, add one and divide it by 2. The function does not return anything. In the main function, write a program that asks for an integer input and call the modify function by passing in the reference of that variable as a parameter. An initial code is provided for you. Just fill in the blanks. Input 1. One line containing an integer input Output Enter a number: 5 Before: 5 After: 3arrow_forwardIn C++, When an array is passed to a function as a pointer, the function doesn't know the size of the array. List 3 ways to handle this problem.arrow_forwardTRUE or FALSE - In C++, a function can't return a pointer. Select one: a.FALSE b.TRUEarrow_forward
- Explain the Pointers and Function Return Values with example.arrow_forwardHow to call function using pointer. Please Explain with Code Example?arrow_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_forward
- Using 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_forwardNote: Write programs in c language format use in programs (Printf & Scanf)arrow_forwardin c++ Create a function that takes in a size and creates an array in the function of that size. The array should only contain even numbers. Return the array to main and show how that is done.arrow_forward
- 3- 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_forwardCreate a function that accepts an array of pointers as an argumentCreate one function that contains only one for loop to print the passed array of pointer.So, lines from //A to //B need to be replaced with only two function calls: one function call to printmonths (from the month array), and the second function call for printing the days (form the dayarray). Both function calls are for the same function but with different parameters.arrow_forwardparameter list can also contain the data type of the output of function : true/false a function declared int addition (int a and b) is capable of returning one value back to the main loop : true/false main () is a void function: true / false the address returned by the reference pointer is always the same regardless of operating system: true/false a function declares as int addition (int a, int b) has a and b as output arguments : true/ falsearrow_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