Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7027639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:16:27+00:00 2026-05-28T00:16:27+00:00

How can I return a struct array from a .Call to a function in

  • 0

How can I return a struct array from a .Call to a function in a C shared library and subsequently use that array in R?

For example:

typedef struct{
    int    thing1;
    int    thing2;
    float  thing3;
    float  thing4;
    char   thing5;
    //... many more things of various simple types
} MY_STRUCT;

SEXP R_Calls_Me(SEXP args) {

    // Do stuff with args...

    // Create arrayOfMyStructs as what type??

    return arrayOfMyStructs;
}

What type is arrayOfMyStructs such that R can use it?

It seems like a common thing one would want to do, but I cannot find any examples of this in the documentation for writing R extensions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:16:28+00:00Added an answer on May 28, 2026 at 12:16 am

    Typically you create list (generic vector) with the components you want to return. In your case something like

    SEXP res = PROTECT(allocVector(VECSXP, 5));
    SET_VECTOR_ELT(res, 0, ScalarInteger(a.thing1));
    SET_VECTOR_ELT(res, 1, ScalarInteger(a.thing2));
    SET_VECTOR_ELT(res, 2, ScalarReal(a.thing3));
    ...
    UNPROTECT(1)
    return res;
    

    It is also customary to assign names to the vector, e.g.:

    const char *names[5] = { "thing1", "thing2", "thing3", "thing4", "thing5" };
    SEXP sNames = PROTECT(allocVector(STRSXP, 5));
    for (int i = 0; i < 5; i++) SET_STRING_ELT(res, i, mkString(names[i]));
    setAttrib(res, R_NamesSymbol, sNames);
    UNPROTECT(1);
    

    Note that what you are describing is not an array but a structure. Arrays are typically much easier to pass as vectors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that can return the difference between the below two dates
Using .NET 4.0 I can quickly convert a struct to and from an array
How can return a variable from a thead (I have the threads handle too).
I know that I can return the index of a particular character of a
I have a method that can return either a single object or a collection
I have some code intended to get a struct from a byte array: public
i am experiencing much trouble reading in files from input into an array struct.
Company struct is defined as follows: typedef struct company { char* company_name; int employee_counter;
I have an array of structs. The struct has two function pointers. Each element
Still working on a problem that started from here Calling C++ dll function from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.