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++: Early Objects (10th Edition)
- 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_forwardProgramming in C. Pointers The purpose of this program is to get familiar with pointers. Swaps Create and use a function that swaps two integers. Ask the user for the values. Display the swapped values. Create another function that swaps two doubles. Ask the user for the values. Display the swapped values. Create yet another function that swaps two strings. Ask the user for the strings Display the swapped values.arrow_forward
- Create an array of four function pointers. Each pointer should point to a different function. Each of these functions should receive two integers and return a float. Using a loop call each of these functions using the addresses present in the array.arrow_forwardCreate a function that can accept a pointers and array with values of 1000, 2, 3, 17, 50. Your program will display the array values and its averagearrow_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_forwardIn C programming: Write a function printAllCourses() which receives an array of course pointers and the array’s size, then prints all courses in the array by calling printCourseRow()arrow_forwardIn C Programming: Write a function inputAllCourses() which receives an array of course pointers and the array’s size, then allows the user to input all courses in the array by calling inputCourse()arrow_forward
- How 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_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
- 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