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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:15:10+00:00 2026-06-12T16:15:10+00:00

I attempting to call into existing C code from Python. The C code defines

  • 0

I attempting to call into existing C code from Python. The C code defines a struct B that contains an struct array of As. The C code also defines a function that puts values into the structure when called. I can access the array member variable, but it is not an list (or something that supports indexing). Instead, I am getting an object that is a proxy for B*.

I found this question, but it doesn’t look like it was completely resolved. I’m also not sure how to make an instance of the Python class B to replace the PyString_FromString().

Below is the code needed to demonstrate my issue and how to execute it:

example.h

typedef struct A_s
{
  unsigned int thing;
  unsigned int other;
} A_t;

typedef struct B_s
{
  unsigned int size;
  A_t items[16];
} B_t;

unsigned int foo(B_t* b);

example.c

#include "example.h"

unsigned int
foo(B_t* b)
{
  b->size = 1;
  b->items[0].thing = 1;
  b->items[0].other = 2;
}

example.i

%module example
%{
#define SWIG_FILE_WITH_INIT
#include "example.h"
%}

%include "example.h"

setup.py

from distutils.core import setup, Extension

module1 = Extension('example', sources=['example.c', 'example.i'])
setup(name='Example', version='0.1', ext_modules=[module1])

script.py – This uses library and demonstrates the failure.

import example
b = example.B_t()
example.foo(b)
print b.size
print b.items
for i in xrange(b.size):
    print b.items[i]

How to run everything:

python setup.py build_ext --inplace
mv example.so _example.so
python script.py
  • 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-06-12T16:15:11+00:00Added an answer on June 12, 2026 at 4:15 pm

    you could write some C helper functions like:

    int get_thing(B_t *this_b_t, int idx);
    int get_other(B_t *this_b_t, int idx);
    void set_thing(B_t *this_b_t, int idx, int val);
    void set_other(B_t *this_b_t, int idx, int val);
    

    wrap these and you can then use the pointer that you get from example.B_t() to access values from within your data-structure arrangement, e.g.

    >>> b = example.B_t()
    >>> a_thing = example.get_thing(b, 0)
    >>> example.set_thing(b,0,999)
    

    Hopefully its obvious what the implementation of these C functions should be – if not I could provide an example…

    granted this isn’t as seamless as being able to access C arrays as python lists – you might be able to use typemaps to achieve this but I can’t remember the exact syntax you would need in this instance – you’d have to do a bit of RTFM on the SWIG documentation

    also possible duplicate here: nested structure array access in python using SWIG

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

Sidebar

Related Questions

I'm attempting to build a method call from strings that have been passed into
I'm attempting to call a function in the ROOT plotting package that accepts three
i have been attempting to pass an object into a function that belongs to
I am attempting to call an external function from the press of a button
I am attempting to call some java code from an XPage and was attempting
I am attempting to call a function but the error I am getting is
I am attempting to call the Apache Commons StringUtils.join() method from within a Groovy
I am attempting to call ffmpeg to create an image from a frame in
I am attempting to create a top-level script that will call about 35+ other
I'm attempting to translate the following C# code into IronPython but I don't know

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.