Return array in a function

2018-12-31 08:34发布

I have an array int arr[5] that is passed to a function fillarr(int arr[]):

int fillarr(int arr[])
{
    for(...);
    return arr;
}
  1. How can I return that array?
  2. How will I use it, say I returned a pointer how am I going to access it?

15条回答
大哥的爱人
2楼-- · 2018-12-31 09:34
int *fillarr(int arr[])

You can still use the result like

int *returned_array = fillarr(some_other_array);
if(returned_array[0] == 3)
    do_important_cool_stuff();
查看更多
牵手、夕阳
3楼-- · 2018-12-31 09:36

This is a fairly old question, but I'm going to put in my 2 cents as there are a lot of answers, but none showing all possible methods in a clear and concise manner (not sure about the concise bit, as this got a bit out of hand. TL;DR

查看更多
像晚风撩人
4楼-- · 2018-12-31 09:37

Just define a type[ ] as return value, like:

        private string[] functionReturnValueArray(string one, string two)
    {

        string[] x = {one, two};


        x[0] = "a";
        x[1] = "b";

        return x;
    }

. . . function call:

string[] y;
y = functionReturnValueArray(stringOne, stringTwo)
查看更多
登录 后发表回答