472,352 Members | 1,505 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,352 software developers and data experts.

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

JW
Hi,
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 4138
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

58
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...
8
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...
11
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...
4
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...
12
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
3
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**' ...
2
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...
4
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...
0
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...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.