如何从c中的函数初始化数组返回

时间:2018-06-18 11:34:37

标签: c data-structures

我正在尝试从函数返回数组,并希望在main函数中初始化新数组。

我的代码在代码块中没有显示任何错误,但它也无效。

这是我的代码

function main(splash, args)
      splash:on_request(function (request)
        request:set_proxy{"myIP","myPort"}
    end)
  assert(splash:go(args.url))
  assert(splash:wait(10))
  local get_dimensions = splash:jsfunc([[
    function () {
        var rect = document.getElementsByClassName('ui-chkbox-box')[0].getClientRects()[0];
        return {"x": rect.left, "y": rect.top
  }     }
  ]])

  local dimensions = get_dimensions()
  splash:mouse_click(dimensions.x, dimensions.y)
  -- Wait split second to allow event to propagate.
  splash:wait(5)
  splash:mouse_click(dimensions.x, dimensions.y)
  splash:wait(5)
  return {
    html = splash:html(),
    png = splash:png(),
    har = splash:har(),
  }
end

1 个答案:

答案 0 :(得分:1)

You have not allocated any memory, so the Arr-Pointer in pointing nowhere.

What you probably want to do is

int * ReturnArray()
{
    int *Arr = malloc(5 * sizeof(int));
    int i;
    for(i=0;i<5;i++)
    {
        Arr[i] = i;
    }

    return Arr;
}

However, you need to free() that memory afterwards.