Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Difference between these pointer syntaxes Date: Sat, 20 Aug 2016 15:30:00 -0700 Organization: None to speak of Lines: 84 Message-ID: References: <87k2fby31m.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="10350"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/o0qITogF99koX8BcXWQgg" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:IchSL5cRfPsY6++5hugkRldEOAQ= sha1:WY9Q97/hhJkJus1vAk4YNKKWdNA= Xref: csiph.com comp.lang.c:92552 Les Cargill writes: > Ben Bacarisse wrote: >> Les Cargill writes: [...] >> Also the rule doesn't cover all the cases so it leaves a lot to be >> guessed at. What about >> >> int x[3]; >> int x[3]; > > That's a duplicate? Nicht, nicht, das is verboten :) No, it's legal at file scope. See "tentative definition", N1570 6.9.2. [...] >>> 2) Treating the array base as an lvalue is verboten. >> >> What's the array base? > > printf("%p\n",x); > or > printf("%p\n",&x[0]); // now a pointer... (You need to cast the argument to void* to avoid undefined behavior.) So, what's the array base? [...] > Using "int x[3];" , "x" is not an l-value. Yes it is, if it's the operand of unary & or sizeof. > Trying "x = 42;", you'll get the diagnostic: > > error: lvalue required as left operand of assignment. Because the expression `x` is "converted" ("adjusted" would have been clearer, since it happens at compile time) to a expression of type int*. The resulting expression is not an lvalue. > lvalue expressions are read-write. They have the property "can-be- > assigned-to". No, an lvalue is "an expression (with an object type other than void) that potentially designates an object". The name of a const-qualified object is a non-writable lvalue. So is an array expression that's decayed to a pointer expression. > rvalues can be either read-only or read-write. The standard doesn't use the term "rvalue" except in one footnote, where it says it's the "value of an expression". Oddly, an lvalue is a kind of expression, but an rvalue is a kind of value. [...] > There is also a null type - called void - which is used to pass pointers The word "null" already has several meanings. I suggest that referring to void as a "null type" is not helpful. > around without defining a type for what is pointed to. It's > up to the called code to cast this "void *" to the desired type to then > dereference the pointer, or to simply use the address as an address, int > a system call or library function. > >> Also, pointer *values* can also be deferenced. Presumably you did not >> intended to exclude those. > > I did not so intend, and I'm not seeing where I did :) Nothing > so useless ad a pointer that could not be deferenced. You can do other things with pointers (pointer values, yes?) than dereference them. You can assign them, compare them, pass them as arguments, etc. Consider a null pointer, or a pointer to an element just past the end of an array. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"