-2

I am writing a shared object (.so) in C, and I want to pass a pointer to an array into the program and pass a pointer to a different array back to the calling function.

My main signature is:

int main(int argc, char **argv)

However, I want to pass in a pointer to an array, and in C it's not done in the way functions work in some other languages -- for example, function(*data).

My questions are:

  1. How do I pass an array pointer in?

  2. How do I pass an array pointer back?

I'm new to C (coming from other languages). I've researched it for some time now and all examples show passing and returning a pointer from an inside function other than the main function. The helpful research I have seen says that it's not possible to return a pointer from main, but is there any way to dynamically allocated (malloc) memory in a shared object and return it to the caller?

Thanks for any help on this.

RTC222
  • 2,025
  • 1
  • 20
  • 53
  • 3
    Libraries don't have a `main`. Just define the function in the library like any other you would use and call that function directly. – dbush Sep 22 '19 at 23:13
  • I just had a hunch that it's different, so I just started researching that aspect of it. Thanks for your comment; it's my confirmation that shared objects are different. – RTC222 Sep 22 '19 at 23:14
  • 1
    Declare the functions as `extern "C" int returnsint() {` for example, then you can load them from the library. I'm not sure if it's required or not but it's a good practice to specify the calling convention aswell like `extern "C" int __stdcall returnsint() {`, this will allow applications that were compiled in different compilers and languages to use this library aswell. – Havenard Sep 22 '19 at 23:18
  • Thanks, @Havenard -- do we use stdcall in Linux? I thought that was only Windows. – RTC222 Sep 22 '19 at 23:21
  • Good point. In [this related question](https://stackoverflow.com/questions/3054257/is-there-stdcall-in-linux) they suggest just declaring the keyword as empty, so the calling convention should default to `__cdecl` which I'm pretty sure is the same thing. – Havenard Sep 22 '19 at 23:24
  • Yes, I think it's __cdecl. – RTC222 Sep 22 '19 at 23:25
  • @Havenard No way. `__stdcall` isn't available on GCC or Clang, let alone other compilers. That will just make your code less portable. In addition, this is not a C++ question, so `extern "C"` is not needed. – S.S. Anne Sep 22 '19 at 23:36
  • @RTC222 are you trying to pass and return a pointer to an array or simply a pointer? Of `foo` or `bar`, which is it? `int x[7]; int(*foo(void))[7]{ return &x; } int *bar(void){ return x; }` –  Sep 23 '19 at 03:16

1 Answers1

0

To pass an array in and out:

int arr[6];

void putarr(int x[6])
{
    memcpy(arr, x, sizeof(arr));
}

void *getarr(void)
{
    return arr;
}

If you really want to pass a pointer to an array out (which is the same thing as an array but more troublesome), use this function:

void (*getarr_p(void))[6]
{
    return &arr;
}
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76