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

  • Home
  • SEARCH
  • 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 8857487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:36:05+00:00 2026-06-14T14:36:05+00:00

Recently I’m doing a project which a function is heavily called, so I want

  • 0

Recently I’m doing a project which a function is heavily called, so I want to using C code in this part. I’m a newbie to ctypes, please forgive me if my question is very easy.

Here I have a 2d list in python:

L = [[1],[1,2],[1,2,3]]

I want call a function in C module with it as parameter. Since there is no 2d-list in C I want to convert it to an array of *int.

I don’t want an normal 2D C array because each length of entry is different.

What I’ve done in python part is:

L = [[1],[1,2],[1,2,3]]
entrylist = []
for entry in L:
  c_entry = (ctypes.c_int * len(entry))(*entry) # c_entry is the C array version of entry
  entrylist.append(c_entry)

c_L = (ctypes.POINTER(ctypes.c_int) * len(entrylist))(*entrylist) # create an array of integer pointer, then initial it

c_L is an “LP_c_long_Array_14 object”, when len(L) == 14.

also, I can print it out perfectly by using

for i in range(len(L)):
  for j in range(len(L[i])):
    print(L[i][j], end = ' ')
  print()

On the other hand, in C code, I define my function as:

int fun(int** c_L)
int fun( (int * c_L)[])

neither works. ctypes throws a “Don’t know how to convert parameter 1” Error.

So, please tell me how to make it work? Thank you very much.

  • 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-14T14:36:06+00:00Added an answer on June 14, 2026 at 2:36 pm

    You are almost there, you just need some tweaks.

    import ctypes as C
    
    lib = C.CDLL("libfoo.so")
    
    l = [[1],[1,2],[1,2,3]]
    entrylist = []
    lengths = []
    
    for sub_l in l:
        entrylist.append((C.c_int*len(sub_l))(*sub_l))
        lengths.append(C.c_int(len(sub_l)))
    
    c_l = (C.POINTER(C.c_int) * len(entrylist))(*entrylist)
    c_lengths = ( C.c_int * len(l))(*lengths)
    
    lib.test(c_l, c_lengths, len(l))  #here we also pass the sizes of all the arrays
    

    where the C side of things looks like:

    #include <stdlib.h>
    #include <stdio.h>
    
    int test(int **ar,int *lens,int n_ar){
        int ii,jj,kk;
        for (ii=0;ii<n_ar;ii++){
            for (jj=0;jj<lens[ii];jj++){
                printf("%d\t",ar[ii][jj]);
            }  
            printf("\n");
            fflush(stdout);
        }
        return 0;
    }
    

    If this doesn’t work, then it may be of use to explicitly cast all your arguments to the c library function.

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

Sidebar

Related Questions

recently, while working on a db2 -> oracle migration project, we came across this
Recently, we discovered odd behavior in some old code. This code has worked for
Recently downloaded some code for a minor open-source project related to a small webgame
Recently I've been doing quite the project mostly working with the DateTime class. Now,..
recently, while reading former's code in my current project, I encounter the problems below:
Recently I'm doing some work on RTMP streaming, that is using Flowplayer to integrate
recently I started using storyboard and I've the following situation: I want to set
Recently I've been doing things like this: import Tkinter class C(object): def __init__(self): self.root
Recently I was asked to develop an app, which basically is going to use
Recently, I was writing a class in which I discovered that I could reduce

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.