Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I return an array?

Is there any way to return an array from a function? More specifically, I've created this function:

char bin[8];

for(int i = 7; i >= 0; i--)
{
    int ascii='a';
    if(2^i-ascii >= 0)
    {
        bin[i]='1';
        ascii=2^i-ascii;
    }
    else
    {
        bin[i]='0';
    }
}

and I need a way to return bin[].


1 Answers

You can't do that but you can:

  • return a dynamicaly allocated array - best owned by a smart pointer so that the caller does not have to care about deallocating memory for it - you could also return something like an std::vector this way.
  • populate an array/vector passed to you as an argument by pointer (suggested) or a non const reference.
like image 165
RnR Avatar answered Dec 22 '25 16:12

RnR



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!