Converting null-terminated numpy structured array member to char* for a C callback function

I’m trying (unsuccessfully) to pass a pointer to a numpy structured array null-terminated string member to a native library function with a signature like “void funcname(const char*)”.

I’ve mocked this in the example below with the cfunc “c_callback”. Is there any relatively elegant way to pass the “x” member to the c_callback?

I’ve mucked about trying to nab the pointer with a bunch of lower-level things I understand even less than Python, usually ending up with errors like

mismatch of argument types: [char x 48]* vs int8*

It seems like this interoperability should be relatively simple, I’m probably just missing something obvious. :frowning:

import numpy as np
from numba import types, njit, cfunc

@cfunc(types.void(types.CPointer(types.int8)))
def c_callback(arg):
    pass

@njit
def make_ptr(array):
    # c_callback(array[0]["x"])  # error: mismatch of argument types: [char x 48] vs int8*
    pass

struct_type = np.dtype([("x", "S48")])
arr = np.empty(1, dtype=struct_type)
arr[0]["x"] = "hello"
print(arr)
make_ptr(arr)

if it’s only one value you are trying to pass (not the whole array), why don’t you just pass the value instead of the pointer?

I’m not understanding your question clearly… I’m trying to pass a pointer to the first element of the array, as the native function takes a null-terminated string (pointer to int8)

I’m still probably missing your point… Can you elaborate, maybe with a line of pseudocode?

sorry, I missed the part about this being a native library with a given signature. I thought you had control over that, please ignore my previous comment.

No worries, thanks for responding! I do wish I had control of the native side :slightly_smiling_face: