unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Newbie question regarding REST arguments from C.
@ 2004-06-11  9:38 Noufal Ibrahim
  2004-06-11 13:56 ` Mike Gran
  0 siblings, 1 reply; 3+ messages in thread
From: Noufal Ibrahim @ 2004-06-11  9:38 UTC (permalink / raw)



Hello everyone, 
      I have a question regarding calling C functions with REST
      arguments. 
      
      Basically, I have a 2D array made using 

      (define foo (create-array (make-array 0 10 10))

      And then I do a
      
      (array-set! foo 1 5 5 )

      Now, to access this element from the scheme side of the program, I
      can do 
      (array-ref foo 5 5)
      And I get a 1

      What I want to know is how do I frame the C equivalent of this
      array-ref.

      The problem is that the function needs to accept a REST argument
      that contains the indices of the array. How do I do this? 

      scm_uniform_vector_ref(scm_c_lookup("foo"),SCM_LIST2(SCM_MAKINUM(5),SCM_MAKINUM(5)));
      errors out saying 
      ERROR: Wrong number of arguments to uniform-vector-ref
      
Thank you. 

-- 
Noufal





_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Newbie question regarding REST arguments from C.
  2004-06-11  9:38 Newbie question regarding REST arguments from C Noufal Ibrahim
@ 2004-06-11 13:56 ` Mike Gran
  2004-06-11 14:37   ` Noufal Ibrahim
  0 siblings, 1 reply; 3+ messages in thread
From: Mike Gran @ 2004-06-11 13:56 UTC (permalink / raw)


[-- Attachment #1: Type: text/plain, Size: 1189 bytes --]

--- Noufal Ibrahim <nkv@willers.employees.org> wrote:
> 
>       The problem is that the function needs to accept a REST
> argument
>       that contains the indices of the array. How do I do this? 
> 
>      
>
scm_uniform_vector_ref(scm_c_lookup("foo"),SCM_LIST2(SCM_MAKINUM(5),SCM_MAKINUM(5)));
>       errors out saying 
>       ERROR: Wrong number of arguments to uniform-vector-ref

You're misreading the meaning of the error message.  You've called the
function correctly and made the list correctly.  Its just that
scm_c_lookup doesn't return what you think it does.

The function scm_c_lookup() doesn't return the array itself.  It
returns the variable that contains the array.  From there you need to
use scm_variable_ref() to dereference the variable to get its contents.
 Its kind of like a pointer vs. the data that the pointer points to.

It says you've got "Wrong number of arguments" because the variable
that scm_c_lookup returns isn't an array, it is a single variable. 
Only its contents is an array.

Attached please find an example.


	
		
__________________________________
Do you Yahoo!?
Friends.  Fun.  Try the all-new Yahoo! Messenger.
http://messenger.yahoo.com/ 

[-- Attachment #2: main.c --]
[-- Type: application/octet-stream, Size: 845 bytes --]

#include <stdio.h>
#include <stdlib.h>
#include <libguile.h>


int main(int argc, char **argv)
{
    SCM s_symbol, s_array, s_val;
    int val;
    int i, j;

    scm_init_guile();
    
    /* Set up the Guile array */ 
    scm_c_primitive_load ("data.scm");
    
    /* Lookup the array "foo" defined at top-level */ 
    s_symbol = scm_c_lookup("foo");
    s_array = scm_variable_ref(s_symbol);

    /* Check to see if it is an array */
    if (SCM_ARRAYP (s_array)) {

	for (j = 0; j < 3; j ++) {
	    for (i = 0; i < 3; i ++) {
		/* Get the Guile value at that location in the array */ 
		s_val = scm_uniform_vector_ref(s_array, 
					       SCM_LIST2(SCM_MAKINUM(i), SCM_MAKINUM(j)));
		val = scm_num2int(s_val, 0, "main");
		
		printf("foo(%2d,%2d) = %d\n", i, j, val);
	    }
	}

    }
    printf("\n");
    
    return(EXIT_SUCCESS);
}


[-- Attachment #3: data.scm --]
[-- Type: application/octet-stream, Size: 60 bytes --]

(define foo 
  (make-array 0 3 3))
(array-set! foo 10 2 2)


[-- Attachment #4: Type: text/plain, Size: 140 bytes --]

_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Newbie question regarding REST arguments from C.
  2004-06-11 13:56 ` Mike Gran
@ 2004-06-11 14:37   ` Noufal Ibrahim
  0 siblings, 0 replies; 3+ messages in thread
From: Noufal Ibrahim @ 2004-06-11 14:37 UTC (permalink / raw)
  Cc: guile-user


Thanks Mike. It worked :)

-- 
Noufal




_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2004-06-11 14:37 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-06-11  9:38 Newbie question regarding REST arguments from C Noufal Ibrahim
2004-06-11 13:56 ` Mike Gran
2004-06-11 14:37   ` Noufal Ibrahim

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).