469,644 Members | 1,982 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,644 developers. It's quick & easy.

Passing Arrays (maybe lists) from Python to C

This is my first post here at Bytes. I have been trolling it, mostly with the help of Google searches, for some time now. I have done about all of the searching and reading that I really know how to do, but because I can't seem to locate something that is even close... I am posting... I would like to thank this community, I have received a great deal of help already.

Background: I have a pre-existing C code, that I have written, which works. The only problem with it is it is slow. I have decided to rewrite the two control functions from C to python. I have succeeded in getting most everything set up, but I am having trouble with, what seems to me, very simple things.

In my C code I declare all of my arrays, I do most everything in arrays, up front, and I have preserved that in my Python. I am using c_types and numpy.

Here are some of my array declarations:
Expand|Select|Wrap|Line Numbers
  1. pop_size = gens = ... = c_int
  2. rank = (c_int * 500)()
  3. ...
  4. p1_chromo = p2_chromo = ... = (c_float * 500)()
  5. population = []
The "population" array needs to be a 2D array and is initialized like this:
Expand|Select|Wrap|Line Numbers
  1. for i in range(pop_size)
  2.      population.append([])
  3.      for j in range(n)
  4.          population[i].append(0.0)
I am initializing it this way, because if I used the numpy function "zeros" to return a 2D array full of zeros I had trouble passing it.

Then I go on down and I do various things to the population and all seems to work well. Then i need to pass PART of my 2D population array to a C function.
it looks like this:
Expand|Select|Wrap|Line Numbers
  1. encode(n,population[x1], population[x2], p1_chromo, p2_chromo)
The C function prototype is:
Expand|Select|Wrap|Line Numbers
  1. void encode(int n, float pop1[], float pop2[], float p1_chromo[], float p2_chromo[])
The error that I get, all the time it seems, is ctypes.ArgumentError: argument 2: <type 'exceptions.TypeError'>: Don't know how to convert parameter 2

I understand what the error is telling me. I am trying to put a list into an array of floats. I over came this problem in previous function calls, when I was passing 1D arrays, by using the byref(...) tag, flag or whatever it is called.

I cannot understand a good way to create indexible arrays in python and then pass them to ANSI C funtions to be worked on. Thats my question... A mountain of text for that little line. But I wanted to show what I was doing and where I was coming from. I have read most of numpy's documentation, and pretty much all of the forum posts I could find... Any suggestions would be greatly appreciated... THANKS!!!
Nov 12 '08 #1
1 4328
I have fixed it.... yay!
Nov 14 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

11 posts views Thread by Soeren Sonnenburg | last post: by
3 posts views Thread by Kermit Rose | last post: by
4 posts views Thread by bei | last post: by
11 posts views Thread by Hakusa | last post: by
17 posts views Thread by =?Utf-8?B?U2hhcm9u?= | last post: by
11 posts views Thread by Gordon C | last post: by
30 posts views Thread by Ivan Reborin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.