Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2007-09

handing over array of pchars to C program

4 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Marc Santhoff

Hi,

has anyone managed to build in a pascal program what in C is a

char *strings[]

and use it from C?

In pascal terms this is a single dimensional array aka vector of PChar.

Do I have to make copies of any string or is it sufficient to fill up an array with PChar?

My attemtps all failed up to know ...

TIA,
Marc

Marco van de Voort
to Marc Santhoff
Show quoted text 5 lines
has anyone managed to build in a pascal program what in C is a char *strings[] and use it from C?

Same as char**strings so ppchar.

In pascal terms this is a single dimensional array aka vector of PChar. Do I have to make copies of any string or is it sufficient to fill up an array with PChar?

That depends on what you do with it, like under C, where you can also let it point to e.g. constants using &

My attemtps all failed up to know ...

Have a look at the unixutil unit (most notably stringtoppchar) and the unix unit implementation (exec procedures that use environment mostly)

Note that the unixutil unit was meant only as a temporary helper for unix, and not really meant to be used by endusers. The "leftovers" of the linux->unix unit transform are stored there.

Jonas Maebe
to Marc Santhoff: has anyone managed to build in a pascal program what in C is a char *strings[] and use it…
Marc Santhoff wrote 12 lines
has anyone managed to build in a pascal program what in C is a char *strings[] and use it from C? In pascal terms this is a single dimensional array aka vector of PChar. Do I have to make copies of any string or is it sufficient to fill up an array with PChar?

That completely depends on what the Pascal and C programs do with those pchars afterwards. In general, there's no difference in passing an array of pchars to a C function compared to passing them to a Pascal procedure: just make sure they are not freed before their last use. Only if the C program may free them later, you have to allocate them using malloc/free (although that's unlikely to be the case).

Jonas

Marc Santhoff
to Marco van de Voort: Same as char**strings so ppchar. That depends on what you do with it, like under C,…
Marco van de Voort wrote 7 lines
> has anyone managed to build in a pascal program what in C is a > > char *strings[] > > and use it from C? Same as char**strings so ppchar.

The missing link. :)

Show quoted text 16 lines
> In pascal terms this is a single dimensional array aka vector of PChar. > > Do I have to make copies of any string or is it sufficient to fill up an > array with PChar? That depends on what you do with it, like under C, where you can also let it point to e.g. constants using & > My attemtps all failed up to know ... Have a look at the unixutil unit (most notably stringtoppchar) and the unix unit implementation (exec procedures that use environment mostly) Note that the unixutil unit was meant only as a temporary helper for unix, and not really meant to be used by endusers. The "leftovers" of the linux->unix unit transform are stored there.

But at least it is a nice source of information for learning how to handle some cases.

Thanks,
Marc