473,669 Members | 2,385 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

difference between *ptr++ and ++*ptr ?

Hello,

could someone explain the difference to me inbetween:
*ptr++ and ++*ptr

Thankx a lot..

Jason.
Nov 14 '05 #1
19 14902
Jason wrote:
Hello,

could someone explain the difference to me inbetween:
*ptr++ and ++*ptr


*ptr++ means "Fetch the thing that `ptr' points to, and
increment `ptr' so it points to the next thing."

++*ptr means "Increment the thing `ptr' points to, and
leave `ptr' itself unchanged."

--
Er*********@sun .com
Nov 14 '05 #2
Eric Sosman wrote:
Jason wrote:
Hello,

could someone explain the difference to me inbetween:
*ptr++ and ++*ptr

*ptr++ means "Fetch the thing that `ptr' points to, and
increment `ptr' so it points to the next thing."

++*ptr means "Increment the thing `ptr' points to, and
leave `ptr' itself unchanged."


The second expression is unambiguous even if you don't remember the
precedence rules or evaluation order. When it comes to the first
expression I'd increment ptr in a separate statement (even though most C
programmers love multiple side-effects).
-- August
Nov 14 '05 #3
On Sun, 15 May 2005 15:09:49 GMT, August Karlstrom
<fu********@com hem.se> wrote:
Eric Sosman wrote:
Jason wrote:
Hello,

could someone explain the difference to me inbetween:
*ptr++ and ++*ptr

*ptr++ means "Fetch the thing that `ptr' points to, and
increment `ptr' so it points to the next thing."

++*ptr means "Increment the thing `ptr' points to, and
leave `ptr' itself unchanged."


The second expression is unambiguous even if you don't remember the
precedence rules or evaluation order. When it comes to the first
expression I'd increment ptr in a separate statement (even though most C
programmers love multiple side-effects).

-- August


Ok - Thank you for the answers

Jason.S.
Nov 14 '05 #4
Eric Sosman wrote:
Jason wrote:

could someone explain the difference to me inbetween:
*ptr++ and ++*ptr


*ptr++ means "Fetch the thing that `ptr' points to, and
increment `ptr' so it points to the next thing."

++*ptr means "Increment the thing `ptr' points to, and
leave `ptr' itself unchanged."


and ++*ptr also fetches the incremented value pointed at.

--
Some informative links:
news:news.annou nce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
Nov 14 '05 #5
August Karlstrom <fu********@com hem.se> writes:
Jason wrote:
could someone explain the difference to me inbetween:
*ptr++ and ++*ptr


The second expression is unambiguous even if you don't remember the
precedence rules or evaluation order. When it comes to the first
expression I'd increment ptr in a separate statement (even though most
C programmers love multiple side-effects).


Each of *ptr++ and ++*ptr has only a single side effect.
--
Ben Pfaff
email: bl*@cs.stanford .edu
web: http://benpfaff.org
Nov 14 '05 #6
August Karlstrom wrote:
Eric Sosman wrote:
Jason wrote:
Hello,

could someone explain the difference to me inbetween:
*ptr++ and ++*ptr

*ptr++ means "Fetch the thing that `ptr' points to, and
increment `ptr' so it points to the next thing."

++*ptr means "Increment the thing `ptr' points to, and
leave `ptr' itself unchanged."


The second expression is unambiguous even if you don't remember the
precedence rules or evaluation order. When it comes to the first
expression I'd increment ptr in a separate statement (even though most C
programmers love multiple side-effects).


`*ptr++` is the standard C idiom for fetch-and-advance-pointer.
Incrementing in a separate statement is likely to make the code more
confusing, I'd have thought.

--
Chris "electric hedgehog" Dollin
"The compiler is free to insert padding because it makes the struct
look bigger and scares away predators." [Keith Thompson, comp.lang.c]
Nov 14 '05 #7
August Karlstrom wrote:

Eric Sosman wrote:
Jason wrote:
Hello,

could someone explain the difference to me inbetween:
*ptr++ and ++*ptr

*ptr++ means "Fetch the thing that `ptr' points to, and
increment `ptr' so it points to the next thing."

++*ptr means "Increment the thing `ptr' points to, and
leave `ptr' itself unchanged."


The second expression is unambiguous even if you don't remember the
precedence rules or evaluation order.


There is no evaluation order.
Assignment is not a sequence point.

This expression
*ptr++
has a value and a side effect.
The side effect is that ptr is incremented.
The value of the expression is the value of what ptr points to,
prior to being incremented,
but that evaluation can be made before
or after the side effect takes place.

--
pete
Nov 14 '05 #8
Ben Pfaff wrote:
Each of *ptr++ and ++*ptr has only a single side effect.


Correct. When `*ptr++' (as well as `++*ptr') is part of a statement
(with assignment, procedure calls etc.) though, it (the statement) will
most likely have multiple side effects. The expression `*ptr++' as
opposed to `++*ptr' doesn't make sense as a single statement (why would
you want to dereference the pointer if you don't use it).

-- August
Nov 14 '05 #9
Chris Dollin wrote:
`*ptr++` is the standard C idiom for fetch-and-advance-pointer.
Incrementing in a separate statement is likely to make the code more
confusing, I'd have thought.


Confusing for the programmer who only has experience with C/C++, maybe.
In almost any other language (procedural at the statement level) the
incrementation is a separate statement.

-- August
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
1506
by: Steven T. Hatton | last post by:
I find writing things such as (*ptr)(arg), and (*ptr), to be at least awkward. I've often thought the following would be useful as an alternative form of ptr->operator(arg), ptr->operator: ptr->(arg) ptr-> Some people may find such an expression unintelligible. If it could be made to work, I believe it would be quickly learned, and probably used by a good number of programmers. Does anybody know of a purely technical reason that
10
3317
by: Bhan | last post by:
Using Ptr of derived class to point to base class and viceversa class base { .... } class derived : public base { .... }
72
3591
by: ravi | last post by:
I have a situation where i want to free the memory pointed by a pointer, only if it is not freed already. Is there a way to know whether the memory is freed or not?
19
3475
by: Rafal Dabrowa | last post by:
What does mean such declaration: void f( char (*ptr) ); Is this the same as void f( char **ptr ); or not ?
27
6670
by: junky_fellow | last post by:
Is *ptr++ equivalent to *(ptr++) ?
25
4806
by: al pacino | last post by:
hi , whats the issue with the following declaration, is it correct and what exactly is happening here. int main() { //..... int* ptr=10; // i suppose ptr is pointing to the memory location which stores the value 10.
6
2247
by: vl106 | last post by:
A static code analysis tool gave me a warning on if (ptr && ptr->data) { ... } I assumed the tool doesn't get the "short circuit behaviour" in the if statement. But a collegue said it may be the missing check ot ptr against NULL.
0
8383
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8587
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8658
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7407
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6210
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2029
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1787
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.