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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:31:39+00:00 2026-06-08T02:31:39+00:00

What I want to do is to convert a C function into python function.

  • 0

What I want to do is to convert a C function into python function.

For example, I have a C function prototype which is pre-defined:

function_test(ViInt32 Arraysize, ViReal64 readingArray[], ViInt32 *Actual_rraysize)

The input of the above function is Arraysize, which is the number of elements in the readingArray parameter.

The outputs of the above function are readingArray[] and Arraysize. readingArray[] returns an array of the result and Actual_arraysize indicates the actual size of the returned array.

And the following is the Python function I wrote by using this C function prototype:

from ctypes import *
def Py_function_test(self, arraysize, readingarray = [], actualarraysize = [-1e-10]):
    while len(readingarray) < arraysize:
        readingarray.append(0)
    _c_actualarraysize = c_int()
    ArrayOfDouble = c_double * arraysize
    _c_readingarray = ArrayOfDouble()
    self.function_test(c_int(arraysize), 
                       byref(_c_readingarray), 
                       byref(_actualarraysize))
    for n in range(arraysize):
        readingarray[n] = _c_readingarray[n]
    actualarraysize[0] = _c_actualarraysize.value

In the end, it turned out the value of acutalarraysize is successfully changed. But the value of readingarray didn’t change after calling the c function prototype.

How to deal with this situation, is it because I should not have used the byref?

  • 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-08T02:31:41+00:00Added an answer on June 8, 2026 at 2:31 am

    Yes, you don’t need byref when passing array. And using list as default argument values will cause unexpected bugs. You can return the array and the actualarrysize in your Python function.

    Here is my code:

    from ctypes import *
    test = cdll.LoadLibrary("test")
    test.function_test.argtypes = [
        c_int, POINTER(c_double), POINTER(c_int)
    ]
    
    def Py_function_test(arraysize):
        _c_readingarray = (c_double*arraysize)()
        _c_actualarraysize = c_int()
        test.function_test(arraysize, _c_readingarray, byref(_c_actualarraysize))
        return _c_readingarray, _c_actualarraysize.value
    
    array, size = Py_function_test(10)
    print list(array), size
    

    the c code:

    void function_test(int Arraysize, double readingArray[], int *Actual_rraysize)
    {
        int i;
        *Actual_rraysize = Arraysize/2;
        for(i=0;i<*Actual_rraysize;i++)
            readingArray[i] = i;
    }
    

    and the output of python code is:

    [0.0, 1.0, 2.0, 3.0, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0] 5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this php function, which I want to convert to javascript function, but
I want to convert the arguments to a function into an associative array with
I want to convert this code into PHP, it's a function that hides layers
I want to convert a column of my dataframe into another one. I have
If I have something like a List[Option[A]] and I want to convert this into
I want to convert a binary number into a float number. Here's an example
Function returns a 4-dimensional array in the form (j,0)(i,0). Want to convert this into
I have a numpy array and want to convert it into an ITK image
I want to convert plaintext to link for an input I have. The HTML
I want to convert XML into binary data in Java? What is the fastest

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.