s-news
[Top] [All Lists]

passing 2d array to C function from S-plus

To: <s-news@lists.biostat.wustl.edu>
Subject: passing 2d array to C function from S-plus
From: "Tom Downing" <td@quaestor.com>
Date: Fri, 22 Feb 2002 18:48:19 -0000
Thread-index: AcG70X8YJWpOumDsQGurSq/gqR9Maw==
Thread-topic: passing 2d array to C function from S-plus
Dear All:

using Visual C++ 6.0 and S+ 6


        I am having trouble figuring out how
to pass a 2d array to a C function, and
receiving a 2d array in return via 
a function addup.  The function addup
simply adds a matrix to itself (I obviously
know this can be done in S). Anyway, the
.dll compiles fine, and I made necessary
changes to header file. The .dll loads
in S, but nithing happens when I run the
S function.    My C program is
probably wrong and perhaps I should be
sending this to a C list, but if anyone
could help me out, I'd greatly appreciate
it.  Thank you very much.


void addup(double **x, long *n, long *k)
{
        long i;
      long j;
        long nrow = *n;
        long ncol = *k;
        for(i=0;i<nrow;i++){
                for(j=0;j<ncol;j++){
                        x[i][j] = x[i][j] * x[i][j];
                }
        }
}



matp <- function(x)
{
        .C("addup",
                x,
                as.integer(nrow(x)),
                as.integer(ncol(x)))
}

is.loaded("addup")
T

x <- as.matrix(rnorm(16),4,4)
storage.mode(x)
"double"
matp(x)
#nothing happens when I run function.

<Prev in Thread] Current Thread [Next in Thread>