![](/images/backgrounds/286549/hp-sunsoft-pascal-4-0-users-manual-549395144x1.png)
6
The commands to compile and execute StrVar.c and
StrVarMain.p
hostname% cc
hostname% a.out abcdefghij abcdefghijklmnopqrtstuvwxyz varstr
length(v) = 6
Incorrect use of string in static variable storage
Avoid constructs that rely on strings being in static variable storage. For example, you could use mktemp(3) in Pascal as follows:
tmp := mktemp('/tmp/eph.xxxxxx')
| This use is incorrect, since mktemp()modifies its argument. Instead, use the C |
| library routine strncpy() (see string(3)) to copy the string constant to a |
| declared char array variable, as in: |
Correct solution using the C |
|
program Use_mktemp ; | |
library routine strncpy() |
|
| procedure strncpy( var dest: univ string ; |
| var srce: univ string ; |
| length: integer) ; external c ; |
| procedure mktemp(var dest: univ string); external c; |
| ... |
| var path: string ; |
| begin |
| ... |
| strncpy( path, '/tmp/eph.xxxxxx', sizeof(path)) ; |
| mktemp( path ) ; |
| ... |
| end . |
|
|
120 | Pascal 4.0 User’s Guide |