473,728 Members | 1,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing pointer to array from C to Python..and modifying same array in python?

Below is a description of what I would like to do. Would someone tell
me if it is possible and if so, how?

I have an array (presumably 'large') that is mallocced in a C function
and its
values are initialized in that same function.
I would like to pass just the pointer to the beginning of the array
from this C function to a Pyton function.
Then the python method can individually access and MODIFY individual
members of the array.
After the call to the python method, the C function will see any
changes to the array.

Can this be done?
I know how to create a PyTuple_New and change each member of the C
array into a PyObject, then add all thos PyObjects to the Tuple,
before passing that PyTyple from C to Python, but this seems slow.

Thanks in advance for your help.
Jul 18 '05 #1
1 4263
On Fri, Jul 11, 2003 at 02:06:10PM -0700, JW wrote:
I have an array (presumably 'large') that is mallocced in a C
function and its values are initialized in that same function. I
would like to pass just the pointer to the beginning of the array
from this C function to a Pyton function. Then the python method
can individually access and MODIFY individual members of the array.
After the call to the python method, the C function will see any
changes to the array. Can this be done?

Of course this can be done, and there are a number of ways to do
it. Based on your implication that there are many more values put in
the array by the C code than will be accessed (or changed) by the
Python code, this method might make sense:

1) have the C function construct a new python object (i.e. the "wrapper")

2) put the real pointer to the C array into the wrapper as instance data

3) register two methods for the object "get()" and "set()", impelemented
in C as Python exported functions.

The get method should take self and the array index as paramaters and
return the values in that slot of the array

The set method should take self, an index, and the values as paramaters
and it should modify the real "C-version" of the array.

4) when you execute your python function, it will be given this
"wrapper" object as a paramater. It can then call into the methods in
the wrapper object to access and change the C-array "in-place" without
converting it at all. Each new value which is set into the array
is converted on the fly.

5) If you like, you can convert get() and set() to __get__() and __set__
so you can use array access syntax to access the array from python.
David Jeske (N9LCA) + http://www.chat.net/~jeske/ + je***@chat.net

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to accomplish. // - - - - - - - - begin code - - - - - - - typedef int sm_t; typedef int bg_t; sm_t sm; bg_t bg;
by: entitledX | last post by:
Hi, I'm trying to use the HDF library to read a few HDF files that I need to process. The data in each file varies in rows, but the columns remain constant. Because of that, I had dynamically allocated a set of pointer to pointers as my multi-dimensional arrays. Here is my code (i have omitted checking calloc's return value to make this shorter): int **filter; filter = calloc( ylength, sizeof(int*) ); for( i = 0 ; i < ylength...
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
by: ZMan | last post by:
The following code won't compile with gcc version 3.4.2 (mingw-special). How come? Error: cannot convert `char (*)' to `char**' /**********************************************************/ #include <cstdio> #define MAX_WORD_LEN 80 #define MAX_SIZE 1000
by: luis | last post by:
I'm using ctypes to call a fortran dll from python. I have no problems passing integer and double arryas, but I have an error with str arrys. For example: ..... StringVector = c_char_p * len(id) # id is a list of strings Id_dat=StringVector() for i in range(len(Id)): ....Id_dat=id
by: digidig | last post by:
Hi, I'm coding a hash algorithm (SHA-1) in C. The algorithm works, everything compiles and I seem to be getting the correct results when I simulate it. I obviously want to automate the checking proceedure so have obtained the necessary results from "hashcalc" and included them as several 20 by 8bit arrays within my code. The hash calc algorithm returns a 5 by 32bit array to an 8bit pointer, I'm then storing the data to a 20 by 8bit array...
by: mjaaland | last post by:
Hi! I've been working with DLLimports passing structs and various other parameters to unmanaged code. I had problems earlier sending pointer to structs to the unmanaged code, and this forum solved it for me (using the ref keyword etc). I now encountered a function that takes a pointer to an array as a parameter, and this array consists of other pointers, to structs. Horrible isn't it? I have no way of modifying the unmanaged code, I...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.