Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Associativity of unary C Operators

Reply
Thread Tools

Associativity of unary C Operators

 
 
Keith Thompson
Guest
Posts: n/a
 
      01-02-2007
"dspfun" <(E-Mail Removed)> writes:
>> > Why does dereferencing p-- work, e.g. *p-- works. But ++p-- doesn't
>> > work.

>>
>> Because ++ takes as its operand an object with scalar type. The expression
>> p-- yields a value, not an object.

>
> So why does it work when you split ++p-- in two different lines?:
> p--;
> ++p;


Please don't snip attribution lines (lines of the form
"So-and-so writes:").

In each of "p--;" and "++p;", the name "p" refers to an object.
The "--" and "++" operators modify an object.

Let's assume that "++p--" is grouped as "++(p--)". I'm not certain
that the grouping is even defined. Then in the subexpression "p--",
"p" refers to an object, but "p--" refers only to a value, the result
of the expression. The prefix "++" operator cannot legally be applied
to an expression that does not designate an object. (The term for an
expression that designates an object is "lvalue".)

If you wonder why a given expression is illegal, think about what it
would mean if it were legal. For example, the assignment operator "="
requires an lvalue as its left operand (that's where the term "lvalue"
comes from). This:
x + 1
is a legal expression, but not an lvalue. This:
x = 42;
is a legal assignment; "x" by itself is an lvalue. But this:
(x + 1) = 42;
is illegal, because "(x + 1)" is not an lvalue. Even if it were
legal, it wouldn't make any sense (except perhaps in a language with a
very different computational model than C's).

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
 
dspfun
Guest
Posts: n/a
 
      01-02-2007
Thanks Richard and Keith for your help and examples!!

 
Reply With Quote
 
 
 
 
Old Wolf
Guest
Posts: n/a
 
      01-02-2007
Richard Heathfield wrote:
> dspfun said:
> >
> > int *p;
> > ~!&*++p--;
> > It doesn't compile, why?

>
> Because ++ only works on objects, and the result of p-- is an expression,


This is not correct. An expression is a sequence of operators etc.
p-- is an expression. Its result is not an expression; its result is
a value. The compile error is because the expression p-- is not an
lvalue expression.

> but not an object.


The more I think about this statement, the more murky it seems.
Better to stick to "lvalue" !

> > The following does compile on the other hand:
> > int *p;
> > p = (int *) = 0x10101010; /*Just to set it to some value..*/

>
> No, it doesn't. Drop the second =.
>
> > ~!&*p--;
> > p results in having the value 0x1010100c which I understand why.

>
> The behaviour is undefined because p's value is changed to an invalid value.


Why do you accept 10101010 as a valid value, but not one less than that?

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      01-03-2007
Old Wolf said:

> Richard Heathfield wrote:
>> dspfun said:
>> >
>> > int *p;
>> > ~!&*++p--;
>> > It doesn't compile, why?

>>
>> Because ++ only works on objects, and the result of p-- is an expression,

>
> This is not correct. An expression is a sequence of operators etc.
> p-- is an expression. Its result is not an expression; its result is
> a value.


If you are right, then the Standard's grammar (which says it is a
postfix-expression) is wrong, and you'd better take that up with ISO.

>> > The following does compile on the other hand:
>> > int *p;
>> > p = (int *) = 0x10101010; /*Just to set it to some value..*/

>>
>> No, it doesn't. Drop the second =.
>>
>> > ~!&*p--;
>> > p results in having the value 0x1010100c which I understand why.

>>
>> The behaviour is undefined because p's value is changed to an invalid
>> value.

>
> Why do you accept 10101010 as a valid value, but not one less than that?


True, I should also have pointed out that the original assignment also
invokes undefined behaviour.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-03-2007
Richard Heathfield <(E-Mail Removed)> writes:
> Old Wolf said:
>> Richard Heathfield wrote:
>>> dspfun said:
>>> >
>>> > int *p;
>>> > ~!&*++p--;
>>> > It doesn't compile, why?
>>>
>>> Because ++ only works on objects, and the result of p-- is an expression,

>>
>> This is not correct. An expression is a sequence of operators etc.
>> p-- is an expression. Its result is not an expression; its result is
>> a value.

>
> If you are right, then the Standard's grammar (which says it is a
> postfix-expression) is wrong, and you'd better take that up with ISO.


p-- is an expression. The *result* of p-- is a value, not an expression.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      01-03-2007
Keith Thompson said:

> Richard Heathfield <(E-Mail Removed)> writes:
>> Old Wolf said:
>>> Richard Heathfield wrote:
>>>> dspfun said:
>>>> >
>>>> > int *p;
>>>> > ~!&*++p--;
>>>> > It doesn't compile, why?
>>>>
>>>> Because ++ only works on objects, and the result of p-- is an
>>>> expression,
>>>
>>> This is not correct. An expression is a sequence of operators etc.
>>> p-- is an expression. Its result is not an expression; its result is
>>> a value.

>>
>> If you are right, then the Standard's grammar (which says it is a
>> postfix-expression) is wrong, and you'd better take that up with ISO.

>
> p-- is an expression. The *result* of p-- is a value, not an expression.


It's late. Or possibly early. My apologies to Old Wolf for incorrectly
rejecting his correction.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
Peter Shaggy Haywood
Guest
Posts: n/a
 
      01-03-2007
Groovy hepcat dspfun was jivin' on 1 Jan 2007 14:02:50 -0800 in
comp.lang.c.
Associativity of unary C Operators's a cool scene! Dig it!

[Snip.]

>Another question, when would you use the unary operator sizeof with
>other unary operators? To save code lines?


I would use other unary operators with sizeof when that is what I
need. For example, to get the size of something pointed to, I would
use the dereferenced pointer as the operand of sizeof. This is often
useful when dynamically allocating memory:

#include <stdlib.h>
some_type *ptr;
ptr = malloc(sizeof *ptr);

This is fine, even though the pointer has not been initialised before
calling malloc(). sizeof's operand is not evaluated, except to
determine the size, so the dereference is not actually performed.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
Reply With Quote
 
dspfun
Guest
Posts: n/a
 
      01-03-2007

Peter "Shaggy" Haywood skrev:

> Groovy hepcat dspfun was jivin' on 1 Jan 2007 14:02:50 -0800 in
> comp.lang.c.
> Associativity of unary C Operators's a cool scene! Dig it!
>
> [Snip.]
>
> >Another question, when would you use the unary operator sizeof with
> >other unary operators? To save code lines?

>
> I would use other unary operators with sizeof when that is what I
> need. For example, to get the size of something pointed to, I would
> use the dereferenced pointer as the operand of sizeof. This is often
> useful when dynamically allocating memory:
>
> #include <stdlib.h>
> some_type *ptr;
> ptr = malloc(sizeof *ptr);
>
> This is fine, even though the pointer has not been initialised before
> calling malloc(). sizeof's operand is not evaluated, except to
> determine the size, so the dereference is not actually performed.


void *p;
printf("%i\",sizeof(*p));

This prints 1, why is the size of a dereferenced void pointer 1?

 
Reply With Quote
 
David Resnick
Guest
Posts: n/a
 
      01-03-2007
dspfun wrote:
>
> void *p;
> printf("%i\",sizeof(*p));
>
> This prints 1, why is the size of a dereferenced void pointer 1?


It isn't. That is a gcc (or whatever compiler you are using)
extension. e.g.

http://gcc.gnu.org/onlinedocs/gcc-4....#Pointer-Arith

-David

 
Reply With Quote
 
dspfun
Guest
Posts: n/a
 
      01-03-2007

dspfun skrev:

> Peter "Shaggy" Haywood skrev:
>
> > Groovy hepcat dspfun was jivin' on 1 Jan 2007 14:02:50 -0800 in
> > comp.lang.c.
> > Associativity of unary C Operators's a cool scene! Dig it!
> >
> > [Snip.]
> >
> > >Another question, when would you use the unary operator sizeof with
> > >other unary operators? To save code lines?

> >
> > I would use other unary operators with sizeof when that is what I
> > need. For example, to get the size of something pointed to, I would
> > use the dereferenced pointer as the operand of sizeof. This is often
> > useful when dynamically allocating memory:
> >
> > #include <stdlib.h>
> > some_type *ptr;
> > ptr = malloc(sizeof *ptr);
> >
> > This is fine, even though the pointer has not been initialised before
> > calling malloc(). sizeof's operand is not evaluated, except to
> > determine the size, so the dereference is not actually performed.

>
> void *p;
> printf("%i\",sizeof(*p));
>
> This prints 1, why is the size of a dereferenced void pointer 1?



printf("%i\n",sizeof(void));

This prints 1, so I guess the size of the void type is 1 byte.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
associativity of operators subramanian100in@yahoo.com, India C Programming 8 03-18-2007 03:35 AM
( ) -> operators associativity confused bochengnever@gmail.com C Programming 2 10-25-2006 02:15 AM
Associativity of unary operators Ashwin N Java 4 09-09-2006 12:59 PM
Ordinary unary function to STL adaptable unary predicate how? SpOiLeR C++ 10 10-19-2005 01:18 PM
Template friend (unary and binary) operators Ruben Campos C++ 5 11-30-2004 05:07 PM



Advertisments