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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:33:37+00:00 2026-06-08T19:33:37+00:00

I have a global variable array in c that I’d like to pull into

  • 0

I have a global variable array in c that I’d like to pull into python. And I’m having difficulties with varout typemap:

/* example.c */
int foo[] = {0, 1};

And here is the very vanilla interface:

/* example.i */
%module example
%{     
 extern int foo[2];
%}

%typemap(varout) int foo[] {
  int i;
  //$1, $1_dim0, $1_dim1
  $result = PyList_New($1_dim0);
  for (i = 0; i < $1_dim0; i++) {
    PyObject *o = PyInt_FromLong((double) $1[i]);
    PyList_SetItem($result,i,o);
  }
}

%include "example.c"

When I try to build it with the following SConstruct:

import distutils.sysconfig
env = Environment(SWIGFLAGS='-python -shadow -Wall'.split(),
                  CPPPATH=[distutils.sysconfig.get_python_inc()],
                  SHLIBPREFIX="")
env.SharedLibrary('_example.so', ['example.c', 'example.i'])

$1_dim0 special variable is not populated, resulting in the following non-compilable code in example_wrap.c:

SWIGINTERN PyObject *Swig_var_foo_get(void) {
  PyObject *pyobj = 0;

  {
    int i;
    //foo, , foo_dim1
    pyobj = PyList_New();
    for (i = 0; i < ; i++) {
      PyObject *o = PyInt_FromLong((double) foo[i]);
      PyList_SetItem(pyobj,i,o);
    }
  }
  return pyobj;
}

So clearly the typemap match has happened, but dimensionality of array is missing. What am I missing? Hard coding the dimension does works.

In general, is there any way to extend global cvar variables with swig?

$ swig -version
SWIG Version 2.0.4
Compiled with g++ [i686-pc-linux-gnu]
Configured options: +pcre
Please see http://www.swig.org for reporting bugs and further information
  • 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-08T19:33:44+00:00Added an answer on June 8, 2026 at 7:33 pm

    You’re almost there with your varout typemap. You need to make two minor changes:

    1. You need to add the size ANY to the int foo[] typemap:

      %typemap(varout) int foo[ANY] {
        int i;
        //$1, $1_dim0, $1_dim1
        $result = PyList_New($1_dim0);
        for (i = 0; i < $1_dim0; i++) {
          PyObject *o = PyInt_FromLong((double) $1[i]);
          PyList_SetItem($result,i,o);
        }
      }
      

      This makes sure your typemap is a match for arrays of (any) known size, not just equivalent to int *foo.

    2. You need to modify example.c to make the size of foo clearer. It’s legal and correct C as it stands but tricky to deduce the size of the array unless you happen to be a complete C compiler. Changing it to:

      int foo[2] = {0, 1};
      

      is sufficient to make sure that it matches the varout typemap.

    With those two changes the generated code works as you’d hope:

    SWIGINTERN PyObject *Swig_var_foo_get(void) {
      PyObject *pyobj = 0;
    
      {
        int i;
        //foo, 2, foo_dim1
        pyobj = PyList_New(2);
        for (i = 0; i < 2; i++) {
          PyObject *o = PyInt_FromLong((double) foo[i]);
          PyList_SetItem(pyobj,i,o);
        }
      }
      return pyobj;
    }
    

    is what gets generated on my machine with those changes.

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

Sidebar

Related Questions

I have a recursion function that parses an object/array with a global variable. If
I have a big array, that I would like to load into memory only
I want to have a global names variable, which looks like that char* names[NAMES_CAP];
I want to have a global array/variable in an iPhone app that uses storyboard.
If i have global variable in A.dll, that depends on global variable in B.dll
I have a global variable averagel that I am trying to manipulate in a
I have a global variable that is an instance of my custom class. How
If I have a global static variable x like in this code #include <stdio.h>
Let's say we have an array defined as a global variable. int array[] =
I have a global variable contacts, var contacts = ContactsApp.getContacts(); I have an KeyUphandler

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.