a dumb strcat question

D

Dave Thompson

Since writing a C program to output "123" is trivial, I assume
you're wanting to write a C function that takes a string such as
'123' and encloses it in quotes: '"123"', like that.
Right. Or at least that's what he asked for. Based on past behavior in
comp.lang.fortran and .pl1 it is unlikely David Frank *wants* to do
anything useful or even correct.
I don't know what 'trim(s1)' does, but I'll assume that 'trim' is
a no-op in this context.
In "basic" Fortran (ISO/IEC 1539-1:1997 aka F95) character strings are
fixed length, (right) padded with blanks. It is often necessary to
remove those blanks, and the instrinsic (builtin) function TRIM does
that, while LEN_TRIM returns the number of characters in a string
excluding trailing blanks i.e. the "valid" length. (An optional "part"
1539-2 adds dynamically varying strings, like C++ std::string or Ada
Unbounded_String or any number of heapptr+count(s) C packages.)

In his example *usage* the (actual) argument is a string literal,
hence (exact length) unpadded, so TRIM was a no-op. More generally it
would have been used on a variable and the TRIM would matter.
So three ways of doing this are <snip usual suspects>

It is true that Fortran has an advantage in this area, in that it can
return as a value an array or a character string -- and in Fortran
like PL/1 and COBOL but unlike C, Pascal and Ada character string is a
distinct type not just array of character -- including with certain
limitations dynamically-determined sizes/lengths of same, without the
programmer having to manage storage allocation. C "don't do dat". You
can work around it more or less well, or bypass it by using your own
string package, but it remains a (tolerable) deficiency.

- David.Thompson1 at worldnet.att.net
 
D

David Frank

It is true that Fortran has an advantage in this area, in that it >
can return as a value an array or a character string
FYI my topic in comp.lang.fortran was
"string function called by C challenge"

After reading Arthur O'Connells reply here, I made a decision
that my C exec interface shown needed a new interface.
Below is how a Fortran function could interface to a DEFAULT
C code calling what it thinks is a C function that returns a
string via adding a argument.

My last message in comp.lang.fortran was posted yesterday in
"string function called by C challenge" topic (see below)
--------------------------------------


Revised TEST.C program with revised interface for enclose
function(s)

afaik, the fortran attribute [C] used is only supported
by MS, CVF, and probably Intel Visual Fortran.

I dont know if F2003 supports with the usual
substitution for Microsoft syntax, anyone know?

main() // TEST.C
{
int c_enclose(char *, char *); // interface C function
int enclose(char *, char *); // interface Fortran function
char c_outs[999];
char outs[999];
int n;

n = c_enclose("the quick brown fox jumped over ",c_outs);
printf("%s%s\n", c_outs,",");

n = enclose("the lazy dog's back. ",outs);
printf("%s%s\n", outs,",");
}

int c_enclose(char *src, char *dst)
{
sprintf(dst, "\'%s\'", src);
return 0;
}

! ------------------------------------
integer function enclose [C] (is1,is2)
integer :: is1, is2 ; pointer (is1,s1), (is2,s2)
character(999) :: s1, s2
integer :: clen

clen = len_trim(s1(1:index(s1,char(0))-1 )) ! trimmed s1 length
s2(1:clen+3) = "'" // s1(1:lens1) // "'" // char(0)
enclose = 0
end function
 
D

David Frank

After reading Arthur O'Connells reply here, I made a decision
that my C exec interface shown needed a new interface.

Correction: it was Arthur J. O'Dwyer's reply what I found
most helpful, thanks Arthur..
 
W

Wolfgang Riedel

To c.l.c:

if you want to see what comes from answering David-Frank-challenges,
feel free to visit c.l.pl1,
otherwise, just don't, please!

(nearly a Trollalert)

Wolfgang
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,122
Latest member
VinayKumarNevatia_
Top