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.
Which one of the following is a true statement about non-generic (non-'void*') pointers?
Choice 1
A pointer to one type may not be cast to a pointer to any other type.
Choice 2
For efficiency, pointer values are always stored in machine registers.
Choice 3
Similarly typed pointers may be added to each other.
Choice 4
Similarly typed pointers may be subtracted from each other.
Choice 5
They are always 32-bit values.
Actually, 3 is not true. In fact it is specifically illegal.
From K&R2, page 103:
Quote:
The valid pointer operations are assignment of pointers of the same type, adding or subtracting a pointer and an integer, subtracting or comparing two pointers to members of the same array, and assigning or comparing to zero. All other pointer arithmetic is illegal.It is not legal to add two pointers
Yeah, its an easy mistake to make because you can add and subtract integers to pointers, but you can only subtract two pointers. It makes sense when you think about it, but it isn't what you would intuitively think.
Not to be too nitpicky, but strictly speaking both of the pointer operations in your test are unsupported. The subtraction of pointers is only meaningful when the pointers are in the same array. Subtraction of pointers in different arrays (as in your test) is undefined. I.e., it won't necessarily be an error but you have no guarantees at all about what you will get.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
You're right my example isn't respecting the same array prerequisite, and was just meant to test the compiler behaviour.
That said, pointers being adresses, substracting two of them gives an offset in the program (possibly virtual) memory addressing space which is not an undefined value, although not terribly useful ...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.