[SOLVED]how to pass `char**` to c static library

Windows specific questions.
Post Reply
objet-a
Posts: 13
Joined: Jul 13, 2021 0:04
Location: Hubei, China

[SOLVED]how to pass `char**` to c static library

Post by objet-a »

Static libraries can print processed data using printf() .
But the main program can not read and output some confusing characters(such as 館?,\u9928\u0017 Look like memory leak).
How to solve it?

1.bas

Code: Select all

Extern "C" Lib "split"
Declare function split(arr as zstring ptr, str as zstring ptr,del as const zstring ptr)as zstring ptr
End Extern
dim a as zstring ptr
a=callocate(10,sizeof(zstring ptr))

dim st as zstring ptr
st=allocate(len("hello|fghfgh"))
*st="hello|fghfgh"
print *split(a,st,"|")
makefile.bat

Code: Select all

gcc -c split.c -o split.o
ar rcs -o libsplit.a split.o
split.c

Code: Select all

#include<string.h>
#include<malloc.h>
#include<stdio.h>
extern char** split(char **arr, char *str, const char *del);
char** split(char **arr, char *str, const char *del)
{
    char *s =NULL;
    s=strtok(str,del);
    printf("LIB address=%llu\n",(unsigned long long)s);
    int i=0;
    while(s != NULL)
    {
        arr[i]=(char*)malloc(sizeof(char)*strlen(s));
        i++;
        *arr++ = s;
        printf("PTR %llu\n",(unsigned long long)&arr[i]);
        s = strtok(NULL,del);
    }
    return &arr[0];
}


Run:
makefile
fbc 1.bas
./1.exe
Last edited by objet-a on Mar 28, 2022 9:15, edited 1 time in total.
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

Re: how to pass `char**` to c static library

Post by angros47 »

In FreeBasic the equivalent of "char**" is "zstring ptr ptr", not "zstring ptr"
objet-a
Posts: 13
Joined: Jul 13, 2021 0:04
Location: Hubei, China

Re: how to pass `char**` to c static library

Post by objet-a »

angros47 wrote: Mar 28, 2022 8:41 In FreeBasic the equivalent of "char**" is "zstring ptr ptr", not "zstring ptr"
Thanks.

Code: Select all

split(a,st,"|")
print *a[1]
After the change, it worked well.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: [SOLVED]how to pass `char**` to c static library

Post by fxm »

Note
To safely allocate a Zstring buffer of 'n' elements, the code is:
Dim As Zstring Ptr pz = Callocate( n + 1, Sizeof( Zstring ) )
Post Reply