Re: [Rd] array indexes in C

From: Thomas Lumley <tlumley_at_u.washington.edu>
Date: Mon 20 Nov 2006 - 16:05:34 GMT

gcc has had variable-length arrays as an extension for a long time. If you look at the notes below the table

   http://gcc.gnu.org/c99status.html
it says "The C99 semantics of variable length arrays (VLAs) are not fully implemented by the existing GCC extension", so it is C99 compliance that is broken.

I still don't see how either gcc or ANSI C99 variable length arrays allow you to treat an argument of type double * as if it was of type double **

         -thomas

On Mon, 20 Nov 2006, Benjamin Tyner wrote:

> Tamas,
>
> I am using GCC with no problems. You can find more examples at
> http://gcc.gnu.org/onlinedocs/gcc-3.3.1/gcc/Variable-Length.html
>
> Ben
>> Hi Ben,
>>
>> Thanks for your answer. I looked at the status of VLA on the GCC
>> homepage and it appears to be "broken". [1] Do you think that the code
>> below still works? Or are you using a different compliler?
>>
>> Thanks,
>>
>> Tamas
>>
>> [1]
http://gcc.gnu.org/c99status.html
>>
>>
>> On Sun, Nov 19, 2006 at 09:55:17AM -0500, Benjamin Tyner wrote:
>>
>>> Tamas,
>>>
>>> You could write convenience functions, but I have used the C99 mechanism
>>> for variable length arrays with no problems calling from R. One thing
>>> you have to keep in mind though is that (as far as I know) the
>>> dimensions must be passed before the array reference. So for example,
>>>
>>> r <- .C("foo",
>>> as.integer(ni),
>>> as.integer(nj),
>>> x = double(ni * nj),
>>> ...)
>>>
>>> with your function defined as
>>>
>>> void foo(int *ni, int *nj, double x[*ni][*nj])
>>> {
>>> ...
>>>
>>> Then in C you can access elements of x via x[3][4], for example.
>>>
>>> Ben
>>>
>>> ______________________________________________
>>> R-devel@r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-devel
>
> ______________________________________________
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley@u.washington.edu	University of Washington, Seattle

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Tue Nov 21 18:18:42 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 21 Nov 2006 - 11:30:48 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.