GigaCoders

Learn. Share. Code

Full width home advertisement

CPP Basics Live Series

Using the Qt Creator

Post Page Advertisement [Top]

I encountered this problem in a personal project of mine. My aim was to pass two variables to a function and have it return three values simultaneously within an array. To return an array in C++ is a little more complex than you would think. To make things more comprehensible for visual learners, I have pictures of the code used in my project and they are accompanied by an abstract description of what is happening. It encompasses just the surface of what pointers can do. More information can be found using the reference links at the end of this post. 

Tap on the picture to get a better view if you are on mobile.


The Function Declaration and return value

On line 42, I created the "policyDetails" pointer variable of type double. The function call named "get_policyDetails" on that same line takes an integer to represent an insurance type and another integer to represent the id number of a customer. The value returned from this function is a pointer so it must be dereferenced to be able to access any of the values saved in memory. Here's how it is done...

Within brackets, place the variable of the returned array and add the index of a value in that array. For example, on line 57, you can recall that the position(index) 0 in an array is the first value. So with that expression, we are trying to get the first value of the array. The last thing is to dereference the expression.

To dereference this expression, you simply put an asterisk(*) before the left bracket. With that, you can assign the entire expression to a new variable if you choose. See lines 57 - 59.

To dereference an integer, float, or double instead of an array, you can place the variable with the return value in brackets and put the asterisk before the left bracket, as shown on lines 45 and 48.


Creating the array/return values


Remember that the return type of this function must be that of the values in your array. In this example, it is a pointer of type double, as demonstrated on line 287. 

On line 312, I used the normal syntax when creating an array, except this array must be declared as static. This is done because we want this array to exist outside the scope of this function. Without it being declared static, that array is deleted as the function finishes execution.

Lines 313 - 315 just entail assigning values to the array. Lastly, you can return that array and review the notes above to understand how to get and use the returned array.


Here is a program for you to run yourself to get a hands-on understanding of how this is done. A summary of this post if you will...

#include <iostream>
using namespace std;

int* function(int num1, int num2, int num3){
    static int arr[3];
    
    arr[0] = num1;
    arr[1] = num2;
    arr[2] = num3;
    
    return arr;
}

int main()
{
    int* get_array = function(4, 5, 6);
    cout<<"1st number is "<<*(get_array + 0)<<endl;
    cout<<"2nd number is "<<*(get_array + 1)<<endl;
    cout<<"3rd number is "<<*(get_array + 2)<<endl;
    
    return 0;
}

References:

Thank you for reading and I hope this helps.

No comments:

Post a Comment

Bottom Ad [Post Page]

| Designed by Colorlib