(a) Can you add an int variable to a pointer?
(b) Can you add a double constant to a pointer?
(c) Can you add two pointers to obtain a third pointer?
(d) Can you multiply a pointer by an int constant?
(e) What is the result of subtracting two pointers, and when is that subtraction legitimate?
Discuss these, please, and not just answer YES or NO.
For example, in (c), can one make any sense of the sum of two pointers? If not, why not?
- InunuLv 51 decade agoFavorite Answer
You can always cast a pointer to int to do all the maths, and convert the result back to pointer. But whether or not it would be a good idea is more questionable. Problem is, you don't know the value of pointers. So what is the point doing any maths over them?
(a) Yes, providing you do the proper type casting. (pointer->int, int addition, int -> pointer)
(b) Yes, providing you do the proper type casting. (pointer->int, double -> int, int addition, int -> pointer)
But it hardly makes any sense since a double variable would not normally hold an index.
(c) Yes, prodiving you do the proper type casting. (pointer->int, int addition, int->pointer).
But it hardly makes any sense since most of the time you have absolutely no idea what value a pointer has.
(d) Yes, same reason as (c).
(e) It will be the distance (i.e. number of bytes) between where the two pointers pointing to. This is called IMDI, "inunu memory differential index", or commonly known as the length.