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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:14:16+00:00 2026-06-08T23:14:16+00:00

I’m have difficulty with a seeming simple problem. There are various C functions that

  • 0

I’m have difficulty with a seeming simple problem. There are various C functions that I need to call from Python code. Currently, I’m trying to do this through ctypes. I’m having issues with a simple example implementation that I’m using to ensure that everything works as intended before changing large chunks of existing code. The C functions take a few parameters, do various mathematical calculations, and then return a double value. This is the C file that I’m using as a test.

#include "Python.h"

double eval(double* args, int length, double* rands, int randLength, int debug)
{
    double val1 = args[0];
    double val2 = rands[0];
    double ret = val1 + val2;
    return ret;
}

In my actual test function I’m printing val1 and val2 to ensure they’re receiving values. Everything is fine on that end. The Python that calls this function is the following.

test = ctypes.CDLL("/home/mjjoyce/Dev/C2M2L/AquaticWavesModel.so")
rand = Random();

args = [2]
argsArr = (ctypes.c_double * len(args))()
argsArr[:] = args

argsRand = [rand.random()]
argsRandArr = (ctypes.c_double * len(argsRand))()
rgsRandArr[:] = argsRand

result = test.eval(argsArr, len(argsArr), argsRandArr, len(argsRandArr), 0)
print "result", result

When this prints the returned result, it usually dumps a large negative number. It’s my assumption that I need to do some converting before Python can handle the value, but I can’t find an answer for that for the life of me. I’ve searched forever, but I’m not certain if I’m missing the obvious, or searching for the wrong information.

Also, if I change the return type of the function to int, the calculations turn out correct and an int is returned and printed as expected. If I set it to double, the calculations are correct in the C code (I print them to check), but Python doesn’t like the returned result.

Any ideas what I’m missing?

  • 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-08T23:14:17+00:00Added an answer on June 8, 2026 at 11:14 pm

    You should set the function’s argument types and return type in Python using the argtypes and restype members:

    test.eval.argtypes = [ctypes.POINTER(ctypes.c_double),
                          ctypes.c_int,
                          ctypes.POINTER(ctypes.c_double),
                          ctypes.c_int,
                          ctypes.c_int]
    test.eval.restype = ctypes.c_double
    

    Then you can call it like you’re doing: construct arrays of type (ctypes.c_double * length) initialized with your values, and the result will be a double.

    See also this ctypes tutorial, it has lots of useful info.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I need to clean up various Word 'smart' characters in user input, including but
I am currently running into a problem where an element is coming back from

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.