pointers

why are the arithmetic operations like division(p/q) and multiplication(p*q) invalid on pointers?.here p and q both are pointers .
What meaning are you going to assign to these operations? For example what is the meaning of p/q?
Last edited on
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
@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.
@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
@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.
@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.