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 5968095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:02:25+00:00 2026-05-22T20:02:25+00:00

I want to access a C function that returns a struct containing double arrays

  • 0

I want to access a C function that returns a struct containing double arrays (where the lengths of these arrays is given by other int members of the struct) from python. The declaration is

typedef struct {
  int dim;
  int vertices;
  int quadrature_degree;
  int polynomial_degree;
  int ngi;
  int quadrature_familiy;
  double *weight; /* 1D: ngi */
  double *l;      /* 2D: ngi * dim */
  double *n;      /* 2D: ngi * vertices */
  double *dn;     /* 3D: ngi * vertices * dim */
} element;

extern void get_element(int dim, int vertices, int quad_degree, int poly_degree, element* e);

The important point is I want to be able to access all the double* members as NumPy arrays of the correct shape (i.e. dn should be a accessible as 3D array).

Simply SWIG-wrapping this gives me the struct just fine, but all the double* members are <Swig Object of type 'double *' at 0x348c8a0> which makes them useless. I played around with the NumPy SWIG interface file but couldn’t get any of the typemaps like ( DATA_TYPE* INPLACE_ARRAY1, int DIM1 ) to work (I think it’s not possible to get them to match in this case but I’d be happy to be proven wrong).

My guess is I’d have to hand code initialization of the NumPy arrays as PyArrayObject for these members and SWIG extend my struct to make them accessible in Python? That looks like a lot of work. Can anyone see a nicer way using SWIG? It would be possible to change the struct or the method returning it if that made things easier.

Alternatively I had a look at cython and ctypes. Would these be better suited for what I’m trying to achieve? I haven’t used cython so can’t judge it’s wrapping capabilities. For ctypes I can roughly imagine how to do it, but it means writing by hand what I had hoped a reasonably automated wrapper could do for me.

Any suggestions gratefully received!

  • 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-22T20:02:26+00:00Added an answer on May 22, 2026 at 8:02 pm

    Using SWIG requires a typemap for the entire struct. Tyepmaps for only the pointer members are not enough, since they don’t have the context to know what size to initialize the NumPy arrays with. I managed to get what I wanted with the following typemaps (which was basically copy & paste from numpy.i and adapt to my needs, probably not very robust):

    %typemap (in,numinputs=0) element * (element temp) {
      $1 = &temp;
    }
    
    %typemap (argout) element * {
      /* weight */
      {
        npy_intp dims[1] = { $1->ngi };
        PyObject * array = PyArray_SimpleNewFromData(1, dims, NPY_DOUBLE, (void*)($1->weight));
        if (!array) SWIG_fail;
        $result = SWIG_Python_AppendOutput($result,array);
      }
      /* l */
      {
        npy_intp dims[2] = { $1->ngi, $1->dim };
        PyObject * array = PyArray_SimpleNewFromData(2, dims, NPY_DOUBLE, (void*)($1->l));
        if (!array) SWIG_fail;
        $result = SWIG_Python_AppendOutput($result,array);
      }
      /* n */
      {
        npy_intp dims[2] = { $1->ngi, $1->vertices };
        PyObject * array = PyArray_SimpleNewFromData(2, dims, NPY_DOUBLE, (void*)($1->n));
        if (!array) SWIG_fail;
        $result = SWIG_Python_AppendOutput($result,array);
      }
      /* dn */
      {
        npy_intp dims[3] = { $1->ngi, $1->vertices, $1->dim };
        PyObject * array = PyArray_SimpleNewFromData(3, dims, NPY_DOUBLE, (void*)($1->dn));
        if (!array) SWIG_fail;
        $result = SWIG_Python_AppendOutput($result,array);
      }
    }
    

    This works different from the C function in that it returns a tuple of NumPy arrays with the data I want, which is more convenient than having to extract it from the element object later. The first typemap furthermore eliminates the need to pass in an object of type element. Hence I can hide the element struct entirely from the python user.

    The python interface finally looks like this:

    weight, l, n, dn = get_element(dim, vertices, quadrature_degree, polynomial_degree)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to program an access function that returns username and password. Here's what
I want to use LabVIEW's Call Library Function Node to access a DLL function,
I have a nested movie clip instance that I want to access. The path
I have a dictionary that goes like this: typedef struct dictNode { int key;
I want to execute stored procedure with one parameter that returns table using EF4
I want to run this function, or at least the bit that deletes the
I have a function that returns a list like this:- List <Column <String1, String2>>
I have a class and in that class there is a function that returns
I want to access the call stack at runtime in a Native C++ application.
I want to access a UIImage programmatically... i.e. myImage.hidden = TRUE; etc. I can

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.