Hi,
Why does this give an error:
int* x;
int* y;
int* z = x+y;
What I mean is: Why is it not allowed to add/multiply/divide Pointers? A int* is data like int is. And don't answer with "Why would you want to?" because that's not the point. I like c because it does what the code says it should, so I'm a bit disappointed now.
Is this general c standart? (Why?)
I am not quite sure but i think it is not allowed simply because it is pointless.
when pointing somewhere you should be sure that this is part of your memory, better said it should be part of the memory the operating system allocated for your programm.
be aware that subtracting pointers is allowed (may be used for finding the array position) so you can hack your way around this problem, but you cannot access the memory on this address (neither read nor write)
1 2 3 4 5 6 7
int* x = newint(12345);
int* y = newint(15912);
int* z = x - (x - y - sizeof(int));
std::cout << "x: " << *x << std::endl;
std::cout << "y: " << *y << std::endl;
std::cout << "z: " << *z << std::endl;
short point: the operator +, * and / aren't overloaded for pointer types
You're not right by saying an int* is data like int is. However, if you really want to use them as ints, you can static_cast<int> them, or probably better; cast them to unsigned long.
I knew that it wasn't very useful, but thought it would work, because c shouldn't care (I mean you also can multiply chars), but
"+, * and / aren't overloaded"
at least explains why it doesn't work. I still think it should be possible.
@Davii: The outcome would be different, even if it would work. the value of x and y are different to &x and &y, even if both values are adresses.
chars can be multiplied because really, they are 8-bit integers (bytes). They are called chars because they are usually used as (string) characters, and that's a bit misleading.
The fact is, if you multiply a pointer, you are most likely doing it by accident.
When overloading the 3 operators and you use them like this:
1 2 3
int* a = newint(20);
int* b = a;
int* c = a + b;
it would be fine, but the addition of pointers seems pointless to me and maybe also to the c-team
since that makes no sence you could overload operator+ like this which would be usefull. template <typename T> T operator+(T* t1, T* t2)) { return *t1 + *t2; }
1 2 3
int* a = newint(20);
int* b = a;
int c = a + b;
This however would confuse the programmer because it makes no sence that adding pointers results in adding the values of the pointers.
So, there would be a problem with both overloads, which makes more sense?
operators that are basically never used or operators that don't represent the real thing?
I think both of them should not be made.
If you find good use for operator+(T* t1, T* t2) you could mail the cplusplus team, they'll take a look at it.
Why is it not allowed to add/multiply/divide Pointers? A int* is data like int is. And don't answer with "Why would you want to?" because that's not the point.
The question isn't "Why would you want to?" The question is "What could it possibly mean to add two pointers? Or multiply them? How could that be a meaningful operation?"
What is it that you would expect to have as the result of adding two addresses? Or of multiplying them?
A pointer is not an integer. It's a pointer. There's a reason they are two distinctly different types in C: they represent 2 completely different things.
You are trying to treat them the same when they are not. Further, what you are trying to do with them makes no conceptual sense.
EDIT:
A int* is data like int is.
everything is data. When you get low enough level, everything without exception is stored as a sequence of bytes.
But that fact shouldn't be abused for nonsensical purposes. Higher level languages like C and C++ are designed to be more conceptual. The whole reason we are given multiple different types, and the ability to create new types, is that it is important for data to be treated differently depending on what it represents.