6.5.6 Additive operators

Previous Table of Contents

1143

additive-expression:
                multiplicative-expression
                additive-expression + multiplicative-expression
                additive-expression - multiplicative-expression

1144 For addition, either both operands shall have arithmetic type, or one operand shall be a pointer to an object type and the other shall have integer type.

1145 (Incrementing is equivalent to adding 1.)

1146 For subtraction, one of the following shall hold:

1147 88) This is often called “truncation toward zero”.

1148 —  both operands have arithmetic type;

1149 —  both operands are pointers to qualified or unqualified versions of compatible object types; or

1150 —  the left operand is a pointer to an object type and the right operand has integer type.

1151 (Decrementing is equivalent to subtracting 1.)

1152 If both operands have arithmetic type, the usual arithmetic conversions are performed on them.

1153 The result of the binary + operator is the sum of the operands.

1154 The result of the binary - operator is the difference resulting from the subtraction of the second operand from the first.

1155 For the purposes of these operators, a pointer to an object that is not an element of an array behaves the same as a pointer to the first element of an array of length one with the type of the object as its element type.

1156 When an expression that has integer type is added to or subtracted from a pointer, the result has the type of the pointer operand.

1157 If the pointer operand points to an element of an array object, and the array is large enough, the result points to an element offset from the original element such that the difference of the subscripts of the resulting and original array elements equals the integer expression.

1158 In other words, if the expression P points to the i-th element of an array object, the expressions (P)+N (equivalently, N+(P)) and (P)−N (where N has the value n) point to, respectively, the i+n-th and i-n-th elements of the array object, provided they exist.

1159 Moreover, if the expression P points to the last element of an array object, the expression (P)+1 points one past the last element of the array object, and if the expression Q points one past the last element of an array object, the expression (Q)−1 points to the last element of the array object.

1160 If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow;

1161 otherwise, the behavior is undefined.

1162 If the result points one past the last element of the array object, it shall not be used as the operand of a unary * operator that is evaluated.

1163 When two pointers are subtracted, both shall point to elements of the same array object, or one past the last element of the array object;

1164 the result is the difference of the subscripts of the two array elements.

1165 The size of the result is implementation-defined, and its type (a signed integer type) is ptrdiff_t defined in the <stddef.h> header.

1166 If the result is not representable in an object of that type, the behavior is undefined.

1167 In other words, if the expressions P and Q point to, respectively, the i-th and j-th elements of an array object, the expression (P)−(Q) has the value i−j provided the value fits in an object of type ptrdiff_t.

1168 Moreover, if the expression P points either to an element of an array object or one past the last element of an array object, and the expression Q points to the last element of the same array object, the expression ((Q)+1)−(P) has the same value as ((Q)−(P))+1 and as −((P)−((Q)+1)), and has the value zero if the expression P points one past the last element of the array object, even though the expression (Q)+1 does not point to an element of the array object.89)

1169 EXAMPLE Pointer arithmetic is well defined with pointers to variable length array types.


        {
                int n = 4, m = 3;
                int a[n][m];
                int (*p)[m] = a;          // p == &a[0]
                p += 1;                   // p == &a[1]
                (*p)[2] = 99;             // a[1][2] == 99
                n = p - a;                // n == 1
        }

If array a in the above example were declared to be an array of known constant size, and pointer p were declared to be a pointer to an array of the same known constant size (pointing to a), the results would be the same.

1170 Forward references: array declarators (6.7.5.2), common definitions <stddef.h> (7.17).

Next

Created at: 2005-06-29 02:19:00 The text from WG14/N1124 is copyright © ISO