From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,45b47ecb995e7a3 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-08-20 22:54:22 PST Path: archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!newsfeed.wirehub.nl!news.stealth.net!204.127.161.2.MISMATCH!wn2feed!worldnet.att.net!135.173.83.71!wnfilter1!worldnet-localpost!bgtnsc05-news.ops.worldnet.att.net.POSTED!not-for-mail From: "David Thompson" Newsgroups: comp.lang.ada References: <3B6F1B2F.4FC3C833@gsde.hou.us.ray.com> <5ee5b646.0108071819.6e84e33d@posting.google.com> <3_Xc7.45$NM5.84779@news.pacbell.net> <3B783712.88029BB8@worldnet.att.net> <3B787A30.F806DB00@home.com> Subject: C strings, was Re: Ada Idioms Progress Preview X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2615.200 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200 Message-ID: Date: Tue, 21 Aug 2001 05:54:21 GMT NNTP-Posting-Host: 12.89.134.193 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc05-news.ops.worldnet.att.net 998373261 12.89.134.193 (Tue, 21 Aug 2001 05:54:21 GMT) NNTP-Posting-Date: Tue, 21 Aug 2001 05:54:21 GMT Organization: AT&T Worldnet Xref: archiver1.google.com comp.lang.ada:12161 Date: 2001-08-21T05:54:21+00:00 List-Id: Warren W. Gay VE3WWG wrote : > .... There are C programs that work with > fixed sized strings, like Ada, though this tends to be rarer (it > sometimes is done with embedded SQL/C). If you then need to pass > the fixed string to a printf() or other function that expects a > "C string", then yes, this then becomes a problem (just as it does > for Ada supplying a string for C). > Actually either truly fixed, or with (variable) length specified by means other than null-termination such as a separate count. That's an unfortunate example; for printf in particular, at least for a data argument not the format string, this is easily handled by the "precision" modifier e.g. %.20s or %.*s if variable. For the many other library and user/3rd-party functions expecting a C (null-terminated) string your point stands. As discussed elsethread I believe, in C++ you also have the option of std::string which effective keeps the separate count and dynamically allocates for you, with methods and standard functions thereon including converstion to C-string. ... > OK, you're saying when you pass arrays into a C function, when > the array is declared external to that function. Something like: > > void func2(char *str) { > // what is the array size of str? > } > void func1() { > char my_array[31]; > func2(my_array); > } > > This is a weakness, but if you know that func2() should work > with fixed length arrays of a certain size, you can use: > > void func2(char str[31]) { > // what is the array size of str? > } > > instead. However, I agree that this is feeble, compared to the > way Ada passes array bounds information. > ... > OK, it sounds like you're suggesting the following: > > void my_func(char *str) { > int slen = sizeof str; // which does not make sense > > But this is nonsense anyway - no self respecting C programmer would > do this, because you are obviously asking for the size of the pointer ;-) > > However, if you declared this instead: > > void my_func(char str[31]) { > int array_len = sizeof str; // this comes close to size of array > > (on many platforms : array_len=32 here due to padding) > NO! In C (or C++) a function parameter declared apparently as an array IS a pointer. Any bound given is ignored except that, at least in C99, if the bound is constant and nonpositive it is clearly a constraint violation, in C90 it was not clear if 6.7.1 adjustment occurred before 6.1.2.5 array type nonempty, and wasn't a mandatory diagnostic anyway; and in C99 if you specify 'static' in the brackets (its fourth! overloaded meaning), you promise to call only with pointers that are in fact to arrays of _at least_ that many elements, allowing some optimizations; but even so: The type IS pointer, and sizeof(str) IS that of pointer. Full stop. C FAQ 6.4, 6.21. What you can do in C if you really want bounds matching is void func2( char (*strp)[31]) { /* array is *strp elements are (*strp)[i] */ /* sizeof(*strp) is 31 */ } void func1(void) { char mystr[31]; char wrong[30]; func2(&mystr); /* OK note & not usually used for "strings" */ func2(&wrong); /* ERROR CAUGHT pointer mismatch */ } This is effectively like classic Pascal. Except for the pointer being explicit, as it always is in C (and the C-like part of C++). In C++ you can use a reference instead of a pointer, and it does require matching of the array bound; and/or templates that deduce the size of an array (and usually inline to a call of a generic implementation passing the bound explicitly). However, where you do have a (nonparameter) array declaration, sizeof the array is exactly sizeof one element times the bound. There may be padding within a struct or union element (and no rep clause to control it, though _some_ compiler have nonportable options or pragmas), but NOT between elements or at the end of an array. And char in particular MUST have sizeof 1 and minimal alignment. -- - David.Thompson 1 now at worldnet.att.net