6.5.6 Additive operators

Previous Table of Contents "New C Standard" commentary

1153

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

1154 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.

1155 (Incrementing is equivalent to adding 1.)

1156 For subtraction, one of the following shall hold:

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

1158 —  both operands have arithmetic type;

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

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

1161 (Decrementing is equivalent to subtracting 1.)

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

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

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

1165 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.

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

1167 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.

1168 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.

1169 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.

1170 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;

1171 otherwise, the behavior is undefined.

1172 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.

1173 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;

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

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

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

1177 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.

1178 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)

1179 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.

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

Next

Created at: 2008-01-30 02:39:43 The text from WG14/N1256 is copyright © ISO