HP C/iX Reference Manual (31506-90011)

74 Chapter5
Expressions
Additive Operators
Additive Operators
The additive operators perform addition (+) and subtraction (-).
Syntax
additive-expression
:=
multiplicative-expression
additive-expression multiplicative-expression
additive-expression
-
multiplicative-expression
Description
The result of the binary addition operator + is the sum of two operands. Both operands
must be arithmetic, or one operand can be a pointer to an object type and the other an
integral type. The usual arithmetic conversions are performed on the operand if both have
arithmetic type. The result is not an lvalue.
If one operand is a pointer and the other operand is an integral type, the integral operand
is scaled by the size of the object pointed to by the pointer. As a result, the pointer is
incremented by an integral number of objects (not just an integral number of storage
units). For example, if p is a pointer to an object of type T, when the value 1 is added to p,
the value of 1 is scaled appropriately. Pointer p will point to the next object of type T. If any
integral value i is added to p, i is also scaled so that p will point to an object that is i
objects away since it is assumed that p actually points into an array of objects of type T.
Use caution with this feature. Consider the case where p points to a structure that is ten
bytes long. Adding a constant 1 to p does not cause p to point to the second byte of the
structure. Rather it causes p to point to the next structure. The value of one is scaled so a
value of ten (the length in bytes of the structure) is used.
The result of the binary subtraction operator - is the difference of the two operands. Both
operands must be arithmetic; the left operand can be a pointer and the right can be an
integral type; or both must be pointers to the same type. The usual arithmetic conversions
are performed on the operands if both have arithmetic type. The result is not an lvalue.
If one operand is a pointer and the other operand is an integral type, the integral operand
is scaled by the size of the object pointed to by the left operand. As a result, the pointer is
decremented by an integral number of objects (not just an integral number of storage
units). See the previous discussion on the addition operator + for more details.
If both operands are pointers to the same type of object, the difference between the
pointers is divided by the size of the object they point to. The result, then, is the integral
number of objects that lie between the two pointers. Given two pointers p and q to the
same type, the difference p-q is an integer k such that adding k to q yields p.
Examples
var1+var2
var1-var2