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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,a1a88c4d509f6381 X-Google-Attributes: gid103376,public From: Hyman Rosen Subject: Re: scope and/or parameters (beginner) Date: 1999/04/15 Message-ID: <37162593.73F3F640@prolifics.com>#1/1 X-Deja-AN: 466795869 Content-Transfer-Encoding: 7bit References: <37064309.889106243@news.dsuper.net> <37084459.8616007@rocketmail.com> <370b0c99.1137352783@news.dsuper.net> <7f2435$54d$1@nnrp1.dejanews.com> <37149AE9.883147B6@prolifics.com> <7f52r9$n7q$1@nnrp1.dejanews.com> X-Accept-Language: en Content-Type: text/plain; charset=us-ascii X-Complaints-To: abuse@panix.com X-Trace: news.panix.com 924198213 1282 209.49.126.226 (15 Apr 1999 17:43:33 GMT) Organization: Prolifics Mime-Version: 1.0 NNTP-Posting-Date: 15 Apr 1999 17:43:33 GMT Newsgroups: comp.lang.ada Date: 1999-04-15T17:43:33+00:00 List-Id: Robert Dewar wrote: > Hyman Rosen wrote: > > Correct. A C programmer would, of course, simply declare > > the parameter as being 'const', then insert the test > > exactly as you did in Ada. > > The "of course" here is mighty forced :-) > > First, I have never *ever* seen C programmers routinely > write prototypes with const in them, as in > > int max (const int a, const int b) { > > so to claim that it is standard practice, as implied by > your "of course" is really stretching things. It's not standard practice to do this because generally no one cares if the parameter is modified. No C programmer thinks of such a parameter as being an alias of some other object. If it was important to preserve the value of the parameter throughout the function, then making it const is the natural solution. > Second, please note that in C, if you *do* declare things > this way, the effect of assigning to the const object is > "undefined" according to the ANSI C standard, not illegal > but undefined! Huh? I think you have a serious misunderstanding. An attempt to assign to a const object is illegal, and any C compiler will prevent you from doing this. What you are thinking of is an attempt to modify an object that is declared const through an alias that is not. Naturally, it may be impossible to detect such an attempt, so the standard makes the effect undefined. Here's an example: void f(const int a) { int *p; a = 1; /* Illegal assignment to constant */ p = &a; /* Illegal - const discarded */ p = (int *)&b; /* Legal because of explicit cast */ *p = 1; /* Undefined result, but compiler need not notice. */ } > perhaps the abbreviation of constant to const is best > interpreted as meaning that it is sort of constant, but > not very :-) :-) C compilers routinely place const global data into readonly segments. I assure you, they are quite constant!