Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: C String terminator Date: Tue, 21 Jan 2020 04:10:18 -0800 Organization: A noiseless patient Spider Lines: 97 Message-ID: <861rrtj9hx.fsf@linuxsc.com> References: <87muaoimkt.fsf@nosuchdomain.example.com> <87a76ofad3.fsf@bsb.me.uk> <87muaodqqn.fsf@bsb.me.uk> <87zhencyud.fsf@bsb.me.uk> <1by2u7s456.fsf@pfeifferfamily.net> <87o8v3cg0i.fsf@bsb.me.uk> <877e1rc6f7.fsf@bsb.me.uk> <87v9p9bylk.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="d01dc2e4789bccc98e4eaed72337c14b"; logging-data="32204"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX190kB40xUUZuFRownxpP154v8nw5Z1fo6E=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:1YIxPSyWTA8tXJ+wQ96bMj+tl/8= sha1:2dfDjHwNjgvHvwZIDp2VuRJktxU= Xref: csiph.com comp.lang.c:149303 Lew Pitcher writes: > Two minor corrections > > Lew Pitcher wrote: > >> T wrote: >> >>> On 2020-01-17 12:48, T wrote: >>> >>>> On 2020-01-17 12:46, Ben Bacarisse wrote: >>>> >>>>> T writes: >>>>> >>>>>> On 2020-01-16 15:45, Ben Bacarisse wrote: >>>>>> >>>>>>> T writes: >>>>>>> >>>>>>>> On 2020-01-16 12:17, Ben Bacarisse wrote: >>>>> >>>>> >>>>> >>>>>>>>> $ gcc -o t t.c >>>>>>>>> $ ./t >>>>> >>>>> >>>>> >>>>>>>> I am a total idiot when it comes to C. How do >>>>>>>> I run this? >>>>>>> >>>>>>> I don't know. That depends on your compiler, OS and so on and that >>>>>>> obviously different to mine sice whole process of compiling and >>>>>>> running is shown above. >>>>>> >>>>>> I have Fedora Linux at my disposal, which has >>>>>> a gcc c compiler. >>>>> >>>>> Then I am baffled. I showed both the gcc command to compile the code, >>>>> and the command to run it. I've removed everything else so you can see >>>>> them. >>>>> >>>>> >>>> >>>> Ben! I am an idiot when it comes to C. You >>>> got to start out like I am a total noob! >>> >>> Okay, I looked at it from a bash standpoint instead >>> of a mystery language I had no clue about. And I >>> feel dumb. You were very precise! >>> >>> $ cd C >>> $ vi t.c >>> $ gcc -o t t.c >>> $ t >>> [FOOp@] >>> >>> >>> Question: why did I get >>> [FOOp@] >>> >>> and you got >>> [FOO] >>> ? >> >> Because, as implied by Ben's source code comment, the program given >> >>>>>>>>> #include >>>>>>>>> >>>>>>>>> void foo(const char *s) >>>>>>>>> { >>>>>>>>> printf("[%s]\n", s); >>>>>>>>> } >>>>>>>>> >>>>>>>>> int main(void) >>>>>>>>> { >>>>>>>>> char str[3] = "FOO"; >>>>>>>>> foo(str); // DANGER, WILL ROBINSON! >>>>>>>>> } >> >> invokes undefined behaviour. >> >> It does this by passing an array of three characters (ultimately) to >> printf(), which /expects/ a C "string" as it's first argument. The array > > Correction #1: in this case, the critical point is that, here, printf() > expects a C "string" as it's second argument (as well as the first argument, > which is the given format string "[%s]\n"). Technically, that isn't right. A %s conversion specifier requires as its argument a pointer to the first element of an array of character type, not a pointer to a string. It is true in this particular case that the array must have a null character in it to avoid undefined behavior, but the argument description says "pointer to array of character type", and does not mention "string". (Also: the possessive of "it" is "its", with no apostrophe. The word "it's", with an apostrophe, is a contraction of "it is".)