Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > is *ptr++ equivalent to *(ptr++)

Reply
Thread Tools

is *ptr++ equivalent to *(ptr++)

 
 
junky_fellow@yahoo.co.in
Guest
Posts: n/a
 
      07-19-2005
Is *ptr++ equivalent to *(ptr++) ?

 
Reply With Quote
 
 
 
 
Rajan
Guest
Posts: n/a
 
      07-19-2005
Yes you are right.

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      07-19-2005
wrote:

> Is *ptr++ equivalent to *(ptr++) ?


Yes. In each case, the associativity is right-to-left and, in each case, the
value seen by * is the old value of ptr, not the new value that it will
have when ++ has completed its work.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
mail: rjh at above domain
 
Reply With Quote
 
ranjeet.gupta@gmail.com
Guest
Posts: n/a
 
      07-19-2005


wrote:
> Is *ptr++ equivalent to *(ptr++) ?


yes it is correct....
one more

++*ptr == (*ptr)++

 
Reply With Quote
 
S.Tobias
Guest
Posts: n/a
 
      07-19-2005
Richard Heathfield <> wrote:
> wrote:
>
>> Is *ptr++ equivalent to *(ptr++) ?

>
> Yes. In each case, the associativity is right-to-left and, in each case, the


I don't think "associativity" is the right word here, as they are
unary operators. "Precedence" is more appropriate, I think.

> value seen by * is the old value of ptr, not the new value that it will
> have when ++ has completed its work.



There's a rule of thumb for unary operators in C, first you read ones
to the right, then ones to the left (unless, of cource, paretheses
override this).

A couple of (non-trivial) examples:
*p++
++p[i];
*p[i]
&a[i]
++p->m
(type*)p->m
*p()

(But I haven't really made sure that it always works; and `sizeof' is
special.)

--
Stan Tobias
mailx `echo LID | sed s/[[:upper:]]//g`
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      07-19-2005
wrote:

>
>
> wrote:
>> Is *ptr++ equivalent to *(ptr++) ?

>
> yes it is correct....
> one more
>
> ++*ptr == (*ptr)++


I hardly think so.

--
Chris "electric hedgehog" Dollin
It's called *extreme* programming, not *stupid* programming.
 
Reply With Quote
 
Eric
Guest
Posts: n/a
 
      07-19-2005
Yes. See the details here:
http://msdn.microsoft.com/library/de....operators.asp
(google with "operator precedence" and "left to right" and "right to left"
for more lists)

Like you can see in the list, the post-increment operator will be executed
first, if there are no brackets.

The associativity for the indirection operator is right to left and for the
post-increment operator left to right. But for this example this is not of
any importance.

Eric


<> wrote in message
news: ps.com...
> Is *ptr++ equivalent to *(ptr++) ?
>



 
Reply With Quote
 
Vijay Kumar R. Zanvar
Guest
Posts: n/a
 
      07-19-2005


Chris Dollin wrote:
> wrote:
>
> >
> >
> > wrote:
> >> Is *ptr++ equivalent to *(ptr++) ?

> >
> > yes it is correct....
> > one more
> >
> > ++*ptr == (*ptr)++

>
> I hardly think so.
>


The two constructs are equivalent in the following scenarios:

int *ptr = ...;
int i;

(void)++*ptr;
(void)(*ptr)++;

However, their behaviour is not the same when used like this:

i = (*ptr)++;
i = ++*ptr;

--
Vijay Kumar R. Zanvar
Home Page - http://geocities.com/vijoeyz/

 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      07-19-2005
Vijay Kumar R. Zanvar wrote:

>
>
> Chris Dollin wrote:
>> wrote:
>>
>> >
>> >
>> > wrote:
>> >> Is *ptr++ equivalent to *(ptr++) ?
>> >
>> > yes it is correct....
>> > one more
>> >
>> > ++*ptr == (*ptr)++

>>
>> I hardly think so.
>>

>
> The two constructs are equivalent in the following scenarios:
>
> int *ptr = ...;
> int i;
>
> (void)++*ptr;
> (void)(*ptr)++;
>
> However, their behaviour is not the same when used like this:
>
> i = (*ptr)++;
> i = ++*ptr;


Hence, they are not equivalent; you can't just use one in place
of the other.

--
Chris "electric hedgehog" Dollin
It's called *extreme* programming, not *stupid* programming.
 
Reply With Quote
 
Vijay Kumar R. Zanvar
Guest
Posts: n/a
 
      07-19-2005


Chris Dollin wrote:
> Vijay Kumar R. Zanvar wrote:
>
> >
> >
> > Chris Dollin wrote:
> >> wrote:
> >>
> >> >
> >> >
> >> > wrote:
> >> >> Is *ptr++ equivalent to *(ptr++) ?


[..]

> Hence, they are not equivalent; you can't just use one in place
> of the other.
>


Yes, you're quite correct. But for a learner, he/she must know the
difference. Practically, these two construct should not be
interchanged.

> --
> Chris "electric hedgehog" Dollin
> It's called *extreme* programming, not *stupid* programming.


 
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
Warning:Xst:382 - Register A is equivalent to B mag VHDL 1 05-19-2005 05:10 PM
instancename of current entity/architecture -- equivalent to C++ this??? Eric Peers VHDL 2 11-18-2004 05:23 PM
VHDL equivalent of verilog trireg Sanjeev VHDL 4 07-23-2004 09:55 AM
equivalent types in different packages Lolo VHDL 3 09-22-2003 03:23 PM
Re: Image Scanning - TWAIN equivalent Brendan Duffy ASP .Net 0 07-24-2003 08:29 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57