Previous Table of Contents "New C Standard" commentary
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
1164
The result of the binary
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
1169
Moreover, if the expression
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
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
1176 If the result is not representable in an object of that type, the behavior is undefined.
1177
In other words, if the expressions
1178
Moreover, if the expression
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
1180
Forward references:
array declarators (6.7.5.2), common definitions
Next
Created at: 2008-01-30 02:39:43
The text from WG14/N1256 is copyright © ISO