How can I return an array?

2020-06-27 06:54发布

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[].

9条回答
别忘想泡老子
2楼-- · 2020-06-27 07:27

You'll want to pass by reference, as follows:

void modifyBin(char (&bin)[8])
{
    /* your function goes here and modifies bin */
}

int main() 
{
    char bin[8];
    modifyBin(bin);
    /* bin has been updated */
    return 0;
}
查看更多
爷、活的狠高调
3楼-- · 2020-06-27 07:28

You are really asking the wrong question. If you want to do string processing in C++, use the std::string and/or std::vector classes, not arrays of char. Your code then becomes:

vector <char> func() {
    vector <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';
       }
    }
    return bin;
}
查看更多
【Aperson】
4楼-- · 2020-06-27 07:33

Your array is a local variable allocated on the stack. You should use new [] to allocate it on the heap. Then you can just say: return bin;. Beware that you will have to explicitly free it with delete [] when you are done with it.

查看更多
我只想做你的唯一
5楼-- · 2020-06-27 07:36

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.
查看更多
We Are One
6楼-- · 2020-06-27 07:36

Similar implemented to @ari's answer, i want to say there is already a boost solution, boost::array solving your problem:

boost::array<char, 8> f() {
    boost::array<char, 8> bin;
    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';
        }
    }
}

...
boost::array<char, 8> a(f());

[I'm not sure what you want to do with that algorithm though, but note that i think you want to do 1 << i (bit-wise shift) instead of 2 ^ i which is not exponentiation in C++.]

Boost array is a normal array, just wrapped in a struct, so you lose no performance what-so-ever. It will also be available in the next C++ version as std::array, and is very easy to do yourself if you don't need the begin()/size()/data()-sugar it adds (to be a container). Just go with the most basic one:

template<typename T, size_t S>
struct array { 
    T t[S];
    T& operator[](ptrdiff_t i) { return t[i]; }
    T const& operator[](ptrdiff_t i) const { return t[i]; }
};

But as usual, use the tools already written by other people, in this case boost::array. It's also got the advantage of being an aggregate (that's why it has no user declared constructor), so it allows initializing with a brace enclosed list:

boost::array<int, 4> a = {{ 1, 2, 3, 4 }};
查看更多
做自己的国王
7楼-- · 2020-06-27 07:41

If you want to return a copy of the array (might make sense for small arrays) and the array has fixed size, you can enclose it in a struct;

struct ArrayWrapper {
   char _bin[8];
};

ArrayWrapper func()
{
    ArrayWrapper x;

    // Do your stuff here using x._bin instead of plain bin

    return x;
}

Or just use a std::vector as has been already suggested.

查看更多
登录 后发表回答