Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get two arrays from function and store them in different data type array in C++

I have a function which contains more than one array and I want to use those two arrays in my main function in this way

void fun ()    //which data type should I place here int OR char ?
{
    int array[4]={1,2,3,4}
    char array2[3]={'a','b','c'}

    return array,array2;
}
int main(){

    int array1[4];
    char array2[3];


    array1=fun();  is it possible to get these array here ?
    array2=fun();
}
like image 872
Black Kite Avatar asked Mar 13 '26 09:03

Black Kite


1 Answers

If you really want to return arrays, you could put them in a std::pair of std::arrays:

#include <array>
#include <utility>

auto fun() {
    std::pair<std::array<int, 4>, std::array<char, 3>> rv{
        {1, 2, 3, 4},
        { 'a', 'b', 'c' }
    };

    return rv;
}

int main() {
    auto[ints, chars] = fun();
} 
like image 98
Ted Lyngmo Avatar answered Mar 14 '26 23:03

Ted Lyngmo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!