BASIC

Author Topic: return a C/C++ array to GLBasic (INLINE)  (Read 4071 times)

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10697
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Hi,

there's always the question if you have an array of some stuff - how do you return that from a C function to GLBasic.
e.g. a SQL function gives you a pointer to the results and the size of the results - how to deal with that?

Here's one solution:
Code: GLBasic [Select]

LOCAL arr$[]
GetArray(arr$[])

FOREACH a$ IN arr$[] // a$ is a reference (pointer) to arr$[x]
        STDOUT a$+"\n"
NEXT
KEYWAIT



FUNCTION foo: // just to close the main() function and write inline between functions.
ENDFUNCTION


INLINE
        // define a prototype for the compiler
        extern "C" void FillArray(const char*** pArrayptr, int* pCount);
ENDINLINE


FUNCTION GetArray: arr$[]
INLINE
        const char** pA;
        int count=0;
        FillArray(&pA, &count);
        // make space to keep a copy of that
        DIM(arr_Str, count);
        for(int i=0; i<count; ++i)
        {
                arr_Str(i) = pA[i]; // that is C++ for GLBasic&#39;s &#39;arr$[i] = xx&#39;
        }
ENDINLINE
ENDFUNCTION



INLINE
// make the function body
// const char*** is:
//  (a pointer*) to (an array*) of (const char*) strings
// int* is a pointer to an integer, returning the array size
extern "C" void FillArray(const char*** pArrayptr, int* pCount)
{
        // make a static array of 4 pointers to const char strings
        static const char* pC[] = {"one o clock", "two o clock", "tick tock", "electic shock"};
        *pArrayptr = pC; // assign the string array pointer
        *pCount = 4; // array has 4 items
}

ENDINLINE
 

Offline Qedo

  • Mr. Polyvector
  • ***
  • Posts: 164
  • UNUS SED LEO
    • View Profile
Re: return a C/C++ array to GLBasic (INLINE)
« Reply #1 on: 2011-Sep-03 »
Good example but it is possible have a example with integer and float array?
Thank you
Ciao

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10697
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: return a C/C++ array to GLBasic (INLINE)
« Reply #2 on: 2011-Sep-05 »
uhm.. it's the same.


Code: GLBasic [Select]
LOCAL arr[]
GetArray(arr[])

FOREACH a IN arr[] // a is a reference (pointer) to arr[x]
        STDOUT a+"\n"
NEXT
KEYWAIT



FUNCTION foo: // just to close the main() function and write inline between functions.
ENDFUNCTION


INLINE
        // define a prototype for the compiler
        extern "C" void FillArray(DGInt* pArrayptr, int* pCount); // DGInt = GLBasic floating point
ENDINLINE


FUNCTION GetArray: arr[]
INLINE
        DGInt* pA;
        int count=0;
        FillArray(&pA, &count);
        // make space to keep a copy of that
        DIM(arr, count);
        for(int i=0; i<count; ++i)
        {
                arr(i) = pA[i]; // that is C++ for GLBasic's 'arr[i] = xx'
        }
ENDINLINE
ENDFUNCTION



INLINE
// make the function body
// int* is a pointer to an integer, returning the array size
extern "C" void FillArray(DGInt** pArrayptr, int* pCount)
{
        // make a static array of 4 pointers to const char strings
        static DGInt pC[] = {1.01, 2.02, 3.03, 4.04};
        *pArrayptr = pC; // assign the string array pointer
        *pCount = 4; // array has 4 items
}

ENDINLINE
 

Offline Qedo

  • Mr. Polyvector
  • ***
  • Posts: 164
  • UNUS SED LEO
    • View Profile
Re: return a C/C++ array to GLBasic (INLINE)
« Reply #3 on: 2011-Sep-05 »
Thank you very much.
Excuse for the obvious question, but for me is not trivial the difference between
const char*** pArrayptr
and
DGInt** pArrayptr
I'm going to study  :)
Ciao

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10697
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: return a C/C++ array to GLBasic (INLINE)
« Reply #4 on: 2011-Sep-06 »
in C, a char* (or C++ const char*) is a primitive type as "double" is one.
const char* means, a pointer (32 bit value holing a memory address) to the first character of a string.
The "const" just is added to disallow changing the value of the string the pointer points to.

So, when you pass a double ** ppD, you have a pointer to a pointer to a double. Arrays in C/C++ are kept by holing a pointer to the first element, so double*. the 2nd * comes from pointing to that pointer, which allows us to "bend" the pointer to point somewhere else ( a new allocated array, e.g.).

Offline naina

  • Mc. Print
  • *
  • Posts: 2
  • Be positive and keep smiling
    • View Profile
Re: return a C/C++ array to GLBasic (INLINE)
« Reply #5 on: 2011-Sep-28 »
Thanks a lot for the above information
Be positive and keep smiling

Offline Qedo

  • Mr. Polyvector
  • ***
  • Posts: 164
  • UNUS SED LEO
    • View Profile
Re: return a C/C++ array to GLBasic (INLINE)
« Reply #6 on: 2011-Sep-28 »
It may seem obvious but it is true. Many thanks for informations and availability
Ciao