How do I convert a Python list into a C array by using ctypes?

No One in Particular :

If I have the follow 2 sets of code, how do I glue them together?

void
c_function(void *ptr) {
    int i;

    for (i = 0; i < 10; i++) {
        printf("%p", ptr[i]);
    }

    return;
}


def python_routine(y):
    x = []
    for e in y:
        x.append(e)

How can I call the c_function with a contiguous list of elements in x? I tried to cast x to a c_void_p, but that didn't work.

I also tried to use something like

x = c_void_p * 10 
for e in y:
    x[i] = e

but this gets a syntax error.

The C code clearly wants the address of an array. How do I get this to happen?

Gabi Purcaru :

The following code works on arbitrary lists:

import ctypes
pyarr = [1, 2, 3, 4]
arr = (ctypes.c_int * len(pyarr))(*pyarr)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python Ctypes: Convert returned C array to python list, WITHOUT numpy

Why is ctypes so slow to convert a Python list to a C array?

Faster way to convert ctypes array to python list?

How do I convert a C# List<string[]> to a Javascript array?

How do I access values stored in an array inside a C struct returned to Python via ctypes?

Python 2D array i C using ctypes

How do I convert multiple lists inside a list using Python?

How to pass a python list to C function (dll) using ctypes

How do I convert an array or a list into a hashref?

How do I format a Python list as an initialized C array?

How do I convert list into dictionary in Python?

Ctypes: fast way to convert a return pointer to an array or Python list

How to convert a ctypes array of c_uint to a numpy array

Passing a python list to "c" DLL function which returns array data using ctypes

How can I convert a BitString to a ctypes Byte Array?

How do I convert a Json file (that contains a array) to a List (of a class object) C# Unity

Convert multiprocessing.Array to ctypes.c_void_p in Python

Efficient way to convert string to ctypes.c_ubyte array in Python

How do I convert a list in a .txt file to a list in Processing (python)?

How do I convert a list of integers into an actual list in python

How do i convert a List<Interface> to List<Class> in c#

How do I convert java.sql.Array to List<MyCustomEnum>?

How do I convert an array list to hash separated string?

how do I convert an object to an array list in JS

How do I convert an array of strings into append-able list?

How do I convert an object array to an object list

How do I convert a named list to an array of objects

How do I split coordinates using python stored in a list into another array/list of their respective axis components

How do I convert a Go array of strings to a C array of strings?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive