title: Pass Pointer to an Array in Haskell to a C Function
body: <p>I have the following C code:</p>
  
  <pre><code>#include &lt;sys/times.h&gt;
  #include &lt;time.h&gt;
  
  
  float etime_( float *tarray )
  {   struct tms buf;
  
      times( &amp;buf );
  
      tarray[0] = 1.0 * buf.tms_utime / CLOCKS_PER_SEC;
      tarray[1] = 1.0 * buf.tms_stime / CLOCKS_PER_SEC;
  
      return tarray[0] + tarray[1];
  }
  </code></pre>
  
  <p>Trying to port this Fortran code to Haskell:</p>
  
  <pre><code>  PROGRAM Test
      IMPLICIT NONE
  
      REAL t, ta(2), etime
  
      INTEGER i
      DOUBLE PRECISION x
  
      do i = 1, 10000
         x = sin( cos( i * 1.0 d0 ) )
         print *, x 
      enddo
  
      ta(1) = 0.0d0
      ta(2) = 0.0d0
  
      t = etime( ta )       
  
      PRINT *, 'user time:    ', ta(1)
      PRINT *, 'system time:  ', ta(2)
      PRINT *, 'process time: ', t
  
    END
  </code></pre>
  
  <p>How can I define array and ! or !!! for the below code to work?</p>
  
  <pre><code>module Main where
  
  import GHC.Ptr
  import GHC.Prim
  import System.IO.Unsafe
  import Control.Monad
  
  foreign import ccall etime_ :: Ptr Double → IO Double
  etime = etime_
  
  main :: IO Int
  main = do
    mapM_ (print . sin . cos . (* (1.0 :: Double)) . fromIntegral) [1..10000 :: Int]
    ta ← array 2
    t ← etime ta
    putStrLn $ "user time:    " ++ show (ta !!! 0)
    putStrLn $ "system time:  " ++ show (ta !!! 1)
    putStrLn $ "process time: " ++ show t
    return 0
  
  array :: Int → IO (Ptr a)
  array size = undefined
  
  (!) :: Ptr a → Int → IO a
  (!) = undefined
  
  (!!!) :: Ptr a → Int → a
  (!!!) = undefined
  </code></pre>