[SOLVED] Pointer arithmetic question : *ptr++ or (*ptr)++
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: RPM Distros,Mostly Mandrake Forks;Drake Tools/Utilities all the way!GO MAGEIA!!!
Posts: 986
Original Poster
Rep:
I already googled it
Quote:
Originally Posted by paulsm4
Hi -
Try it yourself.
Then Google for "operator precedence"
This summarizes how it is explained on the sites that I can find.
Now, let's consider some of the things the above examples have shown us. First off,
consider the fact that *ptr++ is to be interpreted as returning the value pointed to by ptr
and then incrementing the pointer value. This has to do with the precedence of the
operators. Were we to write (*ptr)++ we would increment, not the pointer, but that which
the pointer points to! i.e. if used on the first character of the above example string the 'T'
would be incremented to a 'U'. You can write some simple example code to illustrate this.
The value a pointer holds is another address. So it appears that (*ptr)++ is meant to increment the data that that address stores. If it is an array and it contains a character it would increment a character which of coarse is crazy so I thought I might save myself some trouble and just ask someone. This is what the proceeding explanation states at it's face (literal) level.
Last edited by theKbStockpiler; 06-01-2010 at 02:06 PM.
Reason: the hell of it
Distribution: RPM Distros,Mostly Mandrake Forks;Drake Tools/Utilities all the way!GO MAGEIA!!!
Posts: 986
Original Poster
Rep:
Beginners can't decipher that much code
It looks like they are stating that you can do arithmetic on the values held by the pointers. Is this possible and how is it possible to do this with characters? How do you ++ an "H" or another letter? What does 1 and H equal? Is it 35?
Here's the part from the tutorial again
Now, let's consider some of the things the above examples have shown us. First off,
consider the fact that *ptr++ is to be interpreted as returning the value pointed to by ptr
and then incrementing the pointer value. This has to do with the precedence of the
operators. Were we to write (*ptr)++ we would increment, not the pointer, but that which
the pointer points to! i.e. if used on the first character of the above example string the 'T'
would be incremented to a 'U'. You can write some simple example code to illustrate this.
Thanks in advance
Last edited by theKbStockpiler; 06-01-2010 at 10:15 PM.
Distribution: RPM Distros,Mostly Mandrake Forks;Drake Tools/Utilities all the way!GO MAGEIA!!!
Posts: 986
Original Poster
Rep:
The context is unveiled
Quote:
Originally Posted by jay73
H+1=73, which can also be I depending on how you look at it. That would also be the result of $ + %. Look up the ascii table if you don't get it.
I get it. What is the the purpose of incrementing a character if you don't mind sharing? I don't see it a direct association with a pointer other than it can be done.Maybe if you want to go through the alphabet or something.
...
If it is an array and it contains a character it would increment a character which of coarse is crazy ...
No, it isn't. In abstract/mathematical terms there exist ordered sets, and both, say, natural numbers and ASCII characters are examples of such ordered sets.
So, in ordered sets often increment/decrement operations are defined meaning getting previous/next set element.
Distribution: RPM Distros,Mostly Mandrake Forks;Drake Tools/Utilities all the way!GO MAGEIA!!!
Posts: 986
Original Poster
Rep:
This concept has helped in the overall use of pointers
Quote:
Originally Posted by Sergei Steshenko
No, it isn't. In abstract/mathematical terms there exist ordered sets, and both, say, natural numbers and ASCII characters are examples of such ordered sets.
So, in ordered sets often increment/decrement operations are defined meaning getting previous/next set element.
I concur at this point that it is anything but crazy. Now the purpose of pointers is blatantly apparent.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.