I suspect when for reasons of portability adding pointers is making underlying assumptions regarding the implementation, but can someone please explain why under MinGW GCC I am unable to do the following, due to the following error
"invalid operands to binary
char * and
tbase = ep + tbase;
Yet, if I manipulate the code slightly I can get away with
temp = cbase;
tbase = ep + temp;
I know that I am making some assumptions regarding the pointer implementation but my point is why can't the compiler resolve it for me?
Best How To :
TL;DR - You cannot add two pointers.
Pointer(s) is(are) memory address(es). Just think for a moment, even if you would have been allowed to add two pointers, what is the significance of the result? It will produce mostly an invalid value, isn't it?
OTOH, you can add an
int to a pointer. It is like generating an address by moving that many elements.
Moreover, from the
C11 standard, chapter §6.5.6, Additive operators (emphasis mine)
For addition, either both operands shall have arithmetic type, or one operand shall be a pointer to a complete object type and the other shall have integer type. (Incrementing is equivalent to adding 1.)