2011-01-14 20 views
5

मैं को एक सी फ़ंक्शन में चार पॉइंटर की एक सरणी पास करना चाहता हूं।c_char_p सरणी में आइटम कैसे सम्मिलित करें

मैं http://docs.python.org/library/ctypes.html#arrays

लिए मैं निम्नलिखित कोड लिखने का संदर्भ लें।

from ctypes import * 

names = c_char_p * 4 
# A 3 times for loop will be written here. 
# The last array will assign to a null pointer. 
# So that C function knows where is the end of the array. 
names[0] = c_char_p('hello') 

और मुझे निम्न त्रुटि मिलती है।

TypeError: '_ctypes.PyCArrayType' object does not support item assignment

कोई विचार यह कैसे हल कर सकता हूं? मैं के साथ

c_function(const char** array_of_string); 

उत्तर

12

क्या तुमने किया था एक सरणी प्रकार, न कि वास्तविक सरणी का निर्माण करना था इंटरफेस चाहते हैं, इसलिए मूल रूप से:

import ctypes 
array_type = ctypes.c_char_p * 4 
names = array_type() 

फिर आप की तर्ज पर कुछ कर सकते हैं:

names[0] = "foo" 
names[1] = "bar" 

... और आगे बढ़ने के पैरामीटर के रूप में names सरणी के साथ अपने सी समारोह कॉल करने के लिए।

संबंधित मुद्दे