Path: csiph.com!news.mixmin.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c++ Subject: Re: How do C -programmers do reliable string handling? Date: Mon, 26 Sep 2022 17:54:20 -0700 Organization: None to speak of Lines: 38 Message-ID: <87ill9mybn.fsf@nosuchdomain.example.com> References: <_55YK.371274$SAT4.327548@fx13.iad> MIME-Version: 1.0 Content-Type: text/plain Injection-Info: reader01.eternal-september.org; posting-host="12e4500a99c6636202301e8531a74c4f"; logging-data="4060896"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/3yIm6mjxqklkvAP0rxZpO" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:Jnnbz4O/FbO6swWrUAi2oXjIfKY= sha1:/Nhy2lvky7Eecv3J8/KnEDHl5qk= Xref: csiph.com comp.lang.c++:86631 Richard Damon writes: > On 9/26/22 2:53 PM, JiiPee wrote: >> On 26/09/2022 18:19, Muttley@dastardlyhq.com wrote: >>> Its a bit hard to accidentaly write &(str->length) rather than >>> str->length >> no, I mean if the function takes a pointer (not a variable), and we >> are meant to pass a pointer to the lenght. But we dont know its also >> changing it. >> Lets *assume* that a calculation is done like this: >> float calculate(int round, int* lenght); >> And then you call it with your lenght: >> calculate(1110, &(str->length)); >> thinking that its only reading the lenght. but its changing it also. > > In C, a function taking a pointer is likely going to change it, why > else use a pointer. > > This makes the intent fairly clear. And if the function doesn't intend to change it, it should be a pointer-to-const (which makes good sense if copying the value would be unreasonably expensive or would have side effects). > C++, allowing functions have non-const reference parameters means it > is harder to tell by looking at a call site, what are outputs. And again, you can use a const reference if you don't want to change the value. If a parameter is a non-const pointer (in C or C++) or a non-const reference (in C++), you should assume that the referenced object may be modified. If it isn't, complain to the person who's responsible for the function. > This is handy at times, but means you need to think of things differently. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for Philips void Void(void) { Void(); } /* The recursive call of the void */