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.
Wondering if anyone can clarify something in a text I'm using to learn C (by s. Prata).
Regarding pointer addition, it states:
"You can use the + operator to add an integer to a pointer or a pointer to an integer."
Does this mean only that ptr1 + 3 is same as 3 +
ptr1, or is the author referring to something more like adding values to dereferenced pointers?
No, the author is actually saying that you can add ints to pointers so ptr + 3 is the same as 3 + ptr.
It should be noted that it is not just adding 3 to a number but 3 * sizeof(data type) to ptr. So if ptr points to a data type that is 8 bytes long then ptr + 3 is really (int) ptr + 24 so that ptr + 3 is synonymous with &ptr[3].
---so he is simply affirming the associativity of the addition operator regarding addition of integers to pointers?? If so, then looks like I was confused over a trivial matter!
Last edited by atlantis43; 08-31-2013 at 07:52 AM.
---so he is simply affirming the associativity of the addition operator regarding addition of integers to pointers?? If so, then looks like I was confused over a trivial matter!
I think the word is "commutativity" but yes. In fact, I have read that ptr[3] is equivalent to 3[ptr] but I don't have a reference for that now.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.