pointers

Feb 15, 2013 at 2:10pm
why are the arithmetic operations like division(p/q) and multiplication(p*q) invalid on pointers?.here p and q both are pointers .
Feb 15, 2013 at 2:13pm
What meaning are you going to assign to these operations? For example what is the meaning of p/q?
Last edited on Feb 15, 2013 at 2:13pm
Feb 15, 2013 at 3:07pm
A pointer is an object that points to a location in memory.
So the raw value of a pointer might be something like "00C33FF8" (which will change each time you run the program).
If you multiply a pointer with another pointer, you multiply the addresses together, not the value it points to. So, you will end up with some ambiguous memory address that points to garbage data or a memory address that doesn't belong to the program. The same goes for division. The main idea is, if you multiply or divide two pointers together, it's impossible to know what the result address will point to. So, as vlad has said, there would be no meaning to multiplication and division of pointers.

Adding and subtracting pointers is different, though. Pointing to sequential memory addresses is often useful (arrays).
Last edited on Feb 15, 2013 at 3:23pm
Feb 15, 2013 at 3:21pm
@Thumper
Adding and subtracting pointers is different, though. Pointing to sequential memory addresses is often useful (


You may not add pointers though you cam add pointers with integers. Nevertheless you can apply the unary plus operator to a pointer.
Feb 15, 2013 at 3:23pm
@vlad from moscow
Ah! Thanks for correcting me. That's what I meant.
Incrementing and Decrementing pointers is valuable***

adding and subtracting two pointers falls into the same area as multiplying and dividing.
Last edited on Feb 15, 2013 at 3:41pm
Feb 15, 2013 at 3:32pm
@Thumper
adding and subtracting two pointers falls into the same area as multiplying and dividing


Again you made a slip of the tongue. Substraction of pointers is a valid operation.
Feb 15, 2013 at 3:40pm
@vlad from moscow
And so I learn something new!

Subtraction of two pointers gives a meaningful result: the offset of memory between two pointers. Addition will not give anything meaningful, so it is an invalid operation.

Thanks, btw.
Topic archived. No new replies allowed.