return a C/C++ array to GLBasic (INLINE)

Previous topic - Next topic

Kitty Hello

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's 'arr$[i] = xx'
}
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

Qedo

Good example but it is possible have a example with integer and float array?
Thank you
Ciao

Kitty Hello

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

Qedo

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

Kitty Hello

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.).

naina

Thanks a lot for the above information
Be positive and keep smiling

Qedo

It may seem obvious but it is true. Many thanks for informations and availability
Ciao