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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:54:26+00:00 2026-06-08T21:54:26+00:00

I am trying to replace MATLAB/MEX and switch to Python. I came across SWIG,

  • 0

I am trying to replace MATLAB/MEX and switch to Python. I came across SWIG, ctypes & Cython as possible solutions and started trying out SWIG (which seems very simple).

My C functions have variable argument lengths of the form main(int argc, char *argv[]). I found solutions online, but getting this working with SWIG lead to a lot of issues.

  1. Are the other methods (ctypes / Cython) any simpler?
  2. Any example to do this task with SWIG will be helpful.
  • 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-08T21:54:28+00:00Added an answer on June 8, 2026 at 9:54 pm

    There’s actually an example in the SWIG documentation for exactly this sort of function with Python. I’ve quoted it here with a minor change:

    %typemap(in) (int argc, char *argv[]) {
      int i;
      if (!PyList_Check($input)) {
        PyErr_SetString(PyExc_ValueError, "Expecting a list");
        return NULL;
      }
      $1 = PyList_Size($input);
      $2 = (char **) malloc(($1+1)*sizeof(char *));
      for (i = 0; i < $1; i++) {
        PyObject *s = PyList_GetItem($input,i);
        if (!PyString_Check(s)) {
            free($2);
            PyErr_SetString(PyExc_ValueError, "List items must be strings");
            return NULL;
        }
        $2[i] = PyString_AsString(s);
      }
      $2[i] = 0;
    }
    
    %typemap(freearg) (int argc, char *argv[]) {
       free($2); // If here is uneeded, free(NULL) is legal
    }
    

    This allows you in Python to do simply:

    import test
    test.foo(["a", "b", "c"])
    

    Where test is the name of the module you gave SWIG and foo is a function that matches the signature int argc, char *argv[]. Simple and intuitive to use for a Python programmer and it encapsulates and reuses the complex bit.


    What the documentation doesn’t seem to mention though is that there’s an interface file that does all this for you already:

    %module test
    
    %include <argcargv.i>
    
    %apply (int ARGC, char **ARGV) { (int argc, char *argv[]) }
    
    void foo(int argc, char *argv[]);
    

    is sufficient.

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

Sidebar

Related Questions

I trying to replace string Resources.AppResource.Info; like this Switch(Info); Is this possible with regex?
I'm trying to replace the following: javascript:__doPostBack('ctl33$ShowLists','DoList|2711|311') with main.aspx?action=listshow&id=2711&row=311#auto Only the digits 2711 and
I am trying to replace/modify a part of string in a python file from
I'm trying to replace every multiline import inside a Python source file.. So, the
I'm trying to recursively replace a string with another string in Python. I'm aware
Is it possible to replace all the special characters in a matlab vector through
Trying to replace any non alpha-numeric characters with a hyphen. Can't see why it
I'm trying to replace the top-level navigation of my site with images instead of
I'm trying to replace the zoom functionality with scroll on my microsoft ergonomic keyboard
I am trying to replace a value with a passed value using javascript or

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.