Re: [Haskell-cafe] FFI Newbie: c functions that want pointers to objects

2008-10-16 Thread Creighton Hogg
On Thu, Oct 16, 2008 at 7:28 AM, Mauricio [EMAIL PROTECTED] wrote:
 Hi,

 Some functions in C changes data using pointers,
 like this example:

 void change_int (int *n)
 {
  *n ++;
 }

 What is the proper way to handle that? I
 guess I should wrap it like this:

 foreign ccall change_int change_int
  :: Ptr CInt - IO ()

 Can I use that on a CInt in Haskell code?
 Like this obviously wrong code, but that
 probably shows what I want:

 do
  let myN = 5
  change_int myN

 Thanks,
 MaurĂ­cio

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

(Sending to the list as well as just Mauricio this time...oops)
You are wrapping the function correctly, but you need to actually
create the Ptr CInt using the function malloc from
Foreign.Marshal.Alloc, or more conveniently, the function new from
Foreign.Marshal.Utils.

Your code snippet would become
do
  myNPtr - new 5
  change_int myNPtr
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] FFI Newbie: c functions that want pointers to objects

2008-10-16 Thread Mauricio

Hi,

Some functions in C changes data using pointers,
like this example:

void change_int (int *n)
{
  *n ++;
}

What is the proper way to handle that? I
guess I should wrap it like this:

foreign ccall change_int change_int
 :: Ptr CInt - IO ()

Can I use that on a CInt in Haskell code?
Like this obviously wrong code, but that
probably shows what I want:

do
  let myN = 5
  change_int myN

Thanks,
MaurĂ­cio

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe