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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:05:58+00:00 2026-05-16T22:05:58+00:00

I am still writing on a python interface for my c code with ctypes.

  • 0

I am still writing on a python interface for my c code with ctypes. Today I substituted my file reading function with a python version, which was programmed by somebody else using NumPy. The ‘old’ c version was called with a byref(p_data) while p_data=PFloat() (see below). The main function takes the p_data.

Old file reading:

p_data=POINTER(c_float)
foo.read(filename,byref(p_data))
result=foo.pymain(p_data)

The python file reading function, on the other hand, returns a NumPy array. My question now is:

How do I convert a NumPy array to POINTER(c_float)?

I googled but only found the other way around: C arrays through ctypes accessed as NumPy arrays and things I didn’t understand: C-Types Foreign Function Interface (numpy.ctypeslib)

[update]
corrected a mistake in the example code

  • 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-05-16T22:05:58+00:00Added an answer on May 16, 2026 at 10:05 pm

    Your code looks like it has some confusion in it — ctypes.POINTER() creates a new ctypes pointer class, not a ctypes instance. Anyway, the easiest way to pass a NumPy array to ctypes code is to use the numpy.ndarray‘s ctypes attribute’s data_as method. Just make sure the underlying data is the right type first. For example:

    import ctypes
    import numpy
    c_float_p = ctypes.POINTER(ctypes.c_float)
    data = numpy.array([[0.1, 0.1], [0.2, 0.2], [0.3, 0.3]])
    data = data.astype(numpy.float32)
    data_p = data.ctypes.data_as(c_float_p)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a python function to return the loudness of a .wav file.
I am using python with scipy for writing some code to converge Cartesian coordinates
I'm writing a small wrapper for a C library in Python with Ctypes, and
first, sorry about my english, i still learning. I writing Python module for my
I'm writing a simple version updater in Python, and the regex engine is giving
I'm writing a Python generator which looks like cat. My specific use case is
Disclaimer: i still suck at writing scripts please be gentle if this is a
Still quite new to Haskell.. I want to read the contents of a file,
I'm re-writing a legacy Windows application using Python and running on Linux. Initially, the
Often when I'm using Python I'll find myself writing list comprehensions that look something

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.