Create const char 2D Array to send to C library function

New to FreeBASIC? Post your questions here.
Post Reply
Axle
Posts: 67
Joined: May 31, 2022 6:49
Location: Australia

Create const char 2D Array to send to C library function

Post by Axle »

Hi forum

After searching high and low across the help docs and forum I can't seam to find a solution to this.
Attempting to create the equivalent of C

Code: Select all

const char *TextArray[5] = { "This", "is", "a", "text", "array", };
in FB to send as an argument to a C shared library. I tried using a FB string array, but returns errors.

The FB code:

Code: Select all

dim as string TextArray( 5 ) = { "This", "is", "a", "text", "array", };
MyCFunction( TextArray );
''error 73: Array access, index expected, before ',' in 'MyCFunction( TextArray )'
The C descriptor from the header and calling function from the source:

Code: Select all

int MyCFunction( const char **text);

const char *TextArray[5] = { "This", "is", "a", "text", "array", };
    MyCFunction( TextArray );
Any thoughts on how to create this array in FB with compatible C pointers?
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Create const char 2D Array to send to C library function

Post by fxm »

Have you also tried this ?

Code: Select all

dim as zstring ptr TextArray( 4 ) = { @"This", @"is", @"a", @"text", @"array" }
MyCFunction( @TextArray( 0 ) );
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Create const char 2D Array to send to C library function

Post by dodicat »

I tested in gcc, works fine
test.c

Code: Select all

#include <stdio.h>
int MyCFunction( const char **text);

   
    
    int MyCFunction( const char **text)
    {
    	int j;
    	for (j=0;j<5;j++)
    	printf("%s %s",text[j]," ");
    	return 0;
    }
    
    
   //gcc -c test.c
  //ar rcs -o libtest.a test.o
 
and fb

Code: Select all

#inclib "test"

declare function mycfunction alias "MyCFunction"(as zstring ptr) as long

dim as zstring ptr TextArray( 4 ) = { @"This", @"is", @"a", @"text", @"array" }
mycfunction( @TextArray( 0 ) )
print
sleep

 
result
This is a text array
Axle
Posts: 67
Joined: May 31, 2022 6:49
Location: Australia

Re: Create const char 2D Array to send to C library function

Post by Axle »

Thanks fmx

Compiles now but still get a pointer error:warning 3(2): Passing different pointer types, at parameter 2 of ...

So "dim as zstring ptr array(n) = ( @"n one", @"n two", ...) is the standard way of passing pointers a DLL/SO ?
I am looking through help docs ZSstring Ptr,|Pointer and I'll try some different variations.

This is part my attempts to convert raygui.h examples to raygui.bi (wiitd/raylib-freebasic). Interestingly the example from fb-raylib

Code: Select all

"raygui_controls_test_suite.bas" (raylib 3.5-dev, raygui 2.6-dev) uses the following:
dim as string listViewExList( ... ) = { "This", "is", "a", "list view", "with", "disable", "elements", "amazing!" }
listViewExActive = GuiListViewEx( Rectangle( 165, 180, 140, 200 ), listViewExList(), 8, @listViewExFocus, @listViewExScrollIndex, listViewExActive )
raygui.bi
RAYGUIDEF Function GuiListViewEx overload(bounds As Rectangle, text0 As Const zstring ptr ptr, count as long , focus as long ptr, scrollIndex as long Ptr, active as long) As long
The current raygui.bi V3.2 has changed (Note byval)

Code: Select all

declare function GuiListViewEx(byval bounds as Rectangle, byval text as const zstring ptr ptr, byval count as long, byval focus as long ptr, byval scrollIndex as long ptr, byval active as long) as long
I think this is a problem with the header (byval, byref). I'll continue this with WIITD, or just rework the header.
The info you gave above is still well appreciated as I needed to get some direction on creating C pointers in FB.
Axle
Axle
Posts: 67
Joined: May 31, 2022 6:49
Location: Australia

Re: Create const char 2D Array to send to C library function

Post by Axle »

Hi @fmx, @dodicat. This works.
I missed the pointer index ('@') in MyCFunction( @TextArray( 0 ) ) the first run through, so much apologies.
So solved exactly as fmx posted:
dim as zstring ptr TextArray( 4 ) = { @"This", @"is", @"a", @"text", @"array" }
mycfunction( @TextArray( 0 ) )


C * array and function definition:
const char *TextArray[5] = { "This", "is", "a", "text", "array", };
int MyCFunction( const char **text);

Reading so many different forum suggestions and methods on using zstring ptr I missed it amidst all my tests.
Thank you very much :)
Axle

P.S. My hair is getting too grey for this stuff lol
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Create const char 2D Array to send to C library function

Post by fxm »

Axle wrote: Oct 21, 2022 7:09 P.S. My hair is getting too grey for this stuff lol
I think I'm probably much older (71) than you and the age of having gray or even downright white hair doesn't change that !
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Create const char 2D Array to send to C library function

Post by fxm »

I think it's safer (like the C code) to explicitly add a null pointer (terminating pointer) to the end of the pointer array:
dim as zstring ptr TextArray( 5 ) = { @"This", @"is", @"a", @"text", @"array", 0 }
mycfunction( @TextArray( 0 ) )
Axle
Posts: 67
Joined: May 31, 2022 6:49
Location: Australia

Re: Create const char 2D Array to send to C library function

Post by Axle »

fxm wrote: Oct 21, 2022 9:35 I think it's safer (like the C code) to explicitly add a null pointer (terminating pointer) to the end of the pointer array:
dim as zstring ptr TextArray( 5 ) = { @"This", @"is", @"a", @"text", @"array", 0 }
mycfunction( @TextArray( 0 ) )
Thanks @fmx
The original code from raygui examples creates 9 elements in the array [0] to [8]:

Code: Select all

const char *listViewExList[8] = { "This", "is", "a", "list view", "with", "disable", "elements", "amazing!" };
So I will run with the additional listViewExList[8] = {'\0'} to be safe.

GuiListView() is a wrapper function that splits a single formatted const char * via GuiTextSplit() at ';'||'\n'||'\0' returned as const char ** to GuiListViewEx()
From example.c:

Code: Select all

listViewActive = GuiListView((Rectangle){ 165, 25, 140, 140 }, "Charmander;Bulbasaur;#18#Squirtel;Pikachu;Eevee;Pidgey", &listViewScrollIndex, listViewActive);
listViewExActive = GuiListViewEx((Rectangle){ 165, 180, 140, 200 }, listViewExList, 8, &listViewExFocus, &listViewExScrollIndex, listViewExActive);
raygui.h defines:

Code: Select all

RAYGUIAPI int GuiListView(Rectangle bounds, const char *text, int *scrollIndex, int active);            // List View control, returns selected list item index
RAYGUIAPI int GuiListViewEx(Rectangle bounds, const char **text, int count, int *focus, int *scrollIndex, int active);      // List View with extended parameters
I am re-creating the raylib C examples in Python 3 and FB at the same time so the inside of my head gets a bit mushy after a while lol

Again thanks for the assist :)
I'll give you a heads up and a link when I get these next 4 books done with the examples :)
Post Reply