Create and return a struct with an allocated array

32 views Asked by At

I am trying to create a struct with an array of floats and an array of integers. The created structure should be passed from one function to another. I am not sure what I am doing wrong so any assistance will be helpful. Here is one of the versions that I failed to compile

from libc.stdlib cimport malloc

ctypedef struct Struct:
    float* float_array
    int* int_array

cdef StructNew(int len) -> Struct:
    cdef Struct s
    s.float_array = <float *> malloc(len * sizeof(float))
    s.int_array = <int *> malloc(len * sizeof(int))
    for i in range(len):
        s.float_array[i] = i
        s.int_array[i] = i
    return s

cdef wrap(int wind_len):
    cdef Struct s = StructNew(wind_len)
    for i in range(len):
        print(s.float_array[i])
0

There are 0 answers