1. Passing pointer to c array

I need to create a null-terminated array of strings in Euphoria, and obtain a
pointer to the array.
In C it is:

char *words[] = {
        "Apples", "Oranges", "Lemons", "Pineapple", NULL
                      };

then the pointer words is passed to the listbox function.:
newlistbox(words, coords, click-function)

How do I do this in Euphoria?
Thanks,

Irv

new topic     » topic index » view message » categorize

2. Re: Passing pointer to c array

Irv,
Hopefully noone will throw large, heavy, bladed objects at me if I'm wrong,
but I believe that you can use 0 in place of null when passing to C
functions.

-Allen
On         Wed, 21 Jul 1999 12:09:49 -0400, Irv Mullins wrote:

> I need to create a null-terminated array of strings in Euphoria, and
obtain a
> pointer to the array.
> In C it is:
>
> char *words[] = {
>         "Apples", "Oranges", "Lemons", "Pineapple", NULL
>                       };
>
> then the pointer words is passed to the listbox function.:
> newlistbox(words, coords, click-function)
>
> How do I do this in Euphoria?
> Thanks,
>
> Irv

What has the near functionality of a PDA at a tenth the cost?

...Give Up?

The Game Boy
DragonEagle's Gameboy Developement Page
http://home.earthlink.net/~asoard/gameboy




_______________________________________________________
Get your free, private email at http://mail.excite.com/

new topic     » goto parent     » topic index » view message » categorize

3. Re: Passing pointer to c array

EU>I need to create a null-terminated array of strings in Euphoria, and obtain
EU>pointer to the array.
EU>In C it is:

EU>char *words[] = {
EU>        "Apples", "Oranges", "Lemons", "Pineapple", NULL
EU>                      };

EU>then the pointer words is passed to the listbox function.:
EU>newlistbox(words, coords, click-function)

EU>How do I do this in Euphoria?
EU>Thanks,

EU>Irv

Try:

include machine.e
atom apples, oranges, lemons, pineapple, array
apples = allocate_string("Apples")
oranges = allocate_string("Oranges")
lemons = allocate_string("Lemons")
pineapple = allocate_string("Pineapple")
array = allocate(20)
poke4(array,{apples,oranges,lemons,pineapple,0})

new topic     » goto parent     » topic index » view message » categorize

4. Re: Passing pointer to c array

Irv Mullins wrote:

> I need to create a null-terminated array of strings in
> Euphoria, and obtain a pointer to the array.


WARNING: Untested Code Ahead! Note that the poke4() pokes all the addresses
at once - pretty spiff! A similar trick could be done to gather the
addresses if you knew how many there were (you could poke the count into the
head, and advance the address past it, like a Pascal string), but it's
probably not worth the effort.

To allocate the string array, you could do this:

   function allocateStringArray( sequence s )

      atom address

      -- allocate space for the array pointers, plus a null
      address = allot( length( s ) + 1 ) * 4

      -- for each string in the sequence...
      for i = 1 to length( s ) do

         -- ... replace it with a pointer to itself
         s[i] = allocate_string( s[i] )

      end for

      -- place an end of array marker
      s = append( s, 0 )

      -- poke the addresses into the array.
      poke4( address, s )

      return address

   end function


Of course, you need to destroy the array after you've passed it to the
function, or you'll just be gobbling up memory.

   procedure freeStringArray( atom address )

      atom ptr, strPtr

      -- point to the beginning of the array
      ptr = address

      -- process until end of array
      while 1 do

         -- get the pointer from the array
         strPtr = peek4u( ptr )

         -- end of array?
         if strPtr = 0 then
            exit
         end if

         -- free the pointer
         free( strPtr )

         -- move to the next array element
         ptr += 4

      end while

      -- free the array
      free( address )

   end procedure


Hope this helps!

-- David Cuny

new topic     » goto parent     » topic index » view message » categorize

5. Re: Passing pointer to c array

Thanks Dave, JJ, & Allen - I put together ideas from your replies to get the
following awful simple routine, which works fine: (I know I should de-allocate
the memory,  I'll put it on the to-do list :)

function string_array(sequence list) -- returns pointer to string array
atom a
sequence s
 s = {}
 for i = 1 to length(list) do
     s = append(s,allocate_string(list[i]))
 end for
 s = append(s,0) -- null terminator
 a = allocate(length(s) * 4)
 poke4(a,s)
return a
end function

Now, on to the callbacks!

Irv

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu