[C++-sig] Question about passing ordinary pointers (like int*) to functions
Shandong Han
shandong.han at mizuho-sc.com
Fri Oct 20 02:02:06 CEST 2006
Sorry if this is too naive, but I searched the internet and digged out the
previous mails here without any luck...
I am new to this, and my question is pretty simple: how to call functions
which use a pointer to return value? Sample code is as follows:
===================================
#include "boost/python.hpp"
#include "boost/python/suite/indexing/vector_indexing_suite.hpp"
namespace bp = boost::python;
int add(int a, int b, int* s)
{
*s = a + b;
return 0;
}
BOOST_PYTHON_MODULE(hello){
// bp::def("add", &::add);
{ //::add -- generated using py++
typedef int ( *function_ptr_t )( int,int,int * );
bp::def(
"add"
, function_ptr_t( &::add )
, ( bp::arg("a"), bp::arg("b"), bp::arg("s") )
, bp::default_call_policies() );
}
}
===================================
It builds fine. So test it in python:
>>> from ctypes import *
>>> from hello import *
>>> a=c_int()
>>> add(1,1,byref(a))
Traceback (most recent call last):
File "<stdin>", line 1, in ?
Boost.Python.ArgumentError: Python argument types in
hello.add(int, int, CArgObject)
did not match C++ signature:
add(int a, int b, int * s)
>>>
>>> add(1,1,pointer(a))
Traceback (most recent call last):
File "<stdin>", line 1, in ?
Boost.Python.ArgumentError: Python argument types in
hello.add(int, int, LP_c_long)
did not match C++ signature:
add(int a, int b, int * s)
Maybe the use of ctypes is not the correct way, but I don't know how I can
make a pointer without using that... Could you let me know how to do this?
And another question about arrays:
int arrayTest(int n, const int a[], int b[])
{
for(int i = 0; i < n; i++)
b[i] = a[i] * 2;
return 0;
}
py++ treats the arrays as pointers:
typedef ::std::string ( *function_ptr_t )( ::std::string const & );
bp::def(
"myFunc"
, function_ptr_t( &::myFunc )
, ( bp::arg("arg0") )
, bp::default_call_policies() );
So this becomes the same question as the above one...
Thanks a lot in advance!
S. Han
Note: This e-mail contains privileged and confidential information and is for the sole use of the intended recipient(s). If you are not an intended recipient, you are hereby kindly requested to refrain from printing, copying, or distributing the information contained herein. Furthermore, any other use of the information contained herein is strictly prohibited. If you have received this transmission in error, please kindly notify the sender immediately and destroy all copies of the original message.
More information about the Cplusplus-sig
mailing list