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)
- The Problem: Lots of functions return a pointer when returning a goodvalue and NULL on error. We a quick way of testing for NULL.arrow_forwardWrite a program which will take pointer and display the number on screen. Take number from user and print it on screen using that function.arrow_forwardWrite a C++ function that uses pointer notation only to swap the contents of two integer arrays whose size is equal to SIZE?arrow_forward
- Explaining the importance of the concept of pointer, show how to obtain the address and value of a variable named deg that holds the value 5 in the int data type by using a pointer.arrow_forwardProgram in C. Suppose 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.arrow_forwardProgram In C Create 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 print months (from the month array), and the second function call for printing the days (form the day array). Both function calls are for the same function but with different parameters.arrow_forward
- Explain the general syntax to declare a pointer variable.arrow_forwardC++ Write a program in which a pointer is declared to hold the address of a variable and outputs the address of that variable. Your output should look like this: This address variable named (name of variable) is (address of the variable).arrow_forwardWrite a complete program. In the main function, do the following: Take two integer numbers from user input. Call a function to swap the two numbers using pointers.arrow_forward
- The following is the difference between void and NULL pointers: Use relevant examples to back up your statement.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_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 Learning