A C string (char *
) is an array of char, terminated with a
#0
char.
C library functions require C, not Pascal style string arguments. However, Pascal style strings are automatically converted to C style strings when passed to a routine that expects C style strings. This works only if the routine reads from the string, not if it modifies it.
E.g., this is how you could access the system()
call in your
C library (which is not necessary anymore, since Execute
is
already built-in):
program SysCall; function System (CmdLine: CString): Integer; external name 'system'; var Result: Integer; begin Result := System ('ls -l'); WriteLn ('system() call returned: ', Result) end.
You could use the type PChar
instead of CString
. Both
CString
and PChar
are predefined as ^Char
–
though we recommend CString
because it makes it clearer that
we're talking about some kind of string rather than a single
character.
A lot of library routines in Pascal for many applications exist in
the GPC unit and some other units. Where available, they should be
preferred (e.g. Execute
rather than system()
, and then
you won't have to worry about CString
s.)
Do not pass a C style string as a const
or
var
argument if the C prototype says const char *
or
you will probably get a segfault.