[Info-vax] C... the only winning move is not to play...
JohnF
john at please.see.sig.for.email.com
Tue Feb 11 04:02:33 EST 2014
VAXman- wrote:
> In article <ldaq8t$l3k$1 at reader1.panix.com>, JohnF <john at please.see.sig.for.email.com> writes:
>>{...snip...}
>>> The John way:
>>
>>Nope...
>> char *words[] = { "a", "is", ..., "\000" },
>> *word, string[999]="\000";
>> int length = 0;
>> for ( word=words[0]; *word != '\000'; word++ ) {
>> int len=strlen(word);
>> if ( length+len >= 999 ) break; /* or realloc and cat */
>> strcat(string,word); length+=len; }
>>Or you could alloc/realloc string if you don't like
>>that 999.
>> But you already knew all that, right? You're just
>>intentionally making it look harder that necessary,
>>and maybe a little more vulgar than necessary, too
>>(note: gratuitous vulgarity can sometimes be a
>>giveaway as to what's going on).
>
> No, I was trying to parallel my examples The declaration of the strings was
> not part of the problem; only a way to demonstrate it. If I knew what each
> part of each string was going to be -- as in your example, an array thereof
> -- there would be no reason to even use concat.
>
> The issue is that a number of strings exist as OpenVMS string descriptors.
> I call upon STR$CONCAT to concatanate them
Okay, yeah, if the existing code is already chock-full of descriptors,
then I agree: use the library funcs that can deal with them directly.
And that's especially true if you're intent on staying vms-based anyway.
> because I don't know what each
> string will be. The target string is defined as an OpenVMS dynamic string
> descriptor so it can dynamically resize to whatever is needed to house the
> concatenated parts. OpenVMS and, specifically, the RTL libraries, handle
> the dynamic strings quite well.
>
> STR$CONCAT(&sentence,&word1,&word2,...,&wordN);
>
> ...is a whole world easier on the eyes and more understandably readable
> than:
> int length = 0;
> for ( word=words[0]; *word != '\000'; word++ ) {
> int len=strlen(word);
> if ( length+len >= 999 ) break; /* or realloc and cat */
> strcat(string,word); length+=len; }
Aside: nobody seemed to notice the little bug I embedded in
that snippet, just to see how carefully it was being read.
That word++ won't do the necessary job.
More to the point (even though there's no longer any
real point to deal with), yeah, yours is still more readable,
but I'd assumed your actual situation involved a variable
number of elements to be concatanated, whereby
you'd need some kind of similar loop regardless.
> Anyway, your suggestion worked and for that I am grateful.
Yeah, glad it helped. That other guy's (was it "hb"?) suggestion
to just copy the correct header is probably the generally wiser
solution, rather than embedding this silly-looking kludge into
your application code. But whatever works.
--
John Forkosh ( mailto: j at f.com where j=john and f=forkosh )
More information about the Info-vax
mailing list