I learned by this question that incrementing a NULL pointer or incrementing past the end of an array isn't well-defined behavior:
int* pointer = 0;
int* pointer2 = &a;
pointer2 += 4;
void exampleFunction(int arrayLen, char** strArray)
for(char* str = *strArray; str < *strArray + arrayLen; str++) //or even str < strArray[arrayLen]
//here str is always a pointer to the first char of my string
This is well defined.
A pointer may point to one element past the end of the array. That pointer may not be dereferenced (otherwise that would be undefined behavior) but is can be compared to another pointer within the array.
Section 6.5.6 of the C standard says the following regarding pointer addition:
8 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; otherwise, the behavior is undefined. 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.
Section 6.5.8 says the following regarding pointer comparisons:
5 When two pointers are compared, the result depends on the relative locations in the address space of the objects pointed to. If two pointers to object types both point to the same object, or both point one past the last element of the same array object, they compare equal. If the objects pointed to are members of the same aggregate object, pointers to structure members declared later compare greater than pointers to members declared earlier in the structure, and pointers to array elements with larger subscript values compare greater than pointers to elements of the same array with lower subscript values. All pointers to members of the same union object compare equal. If the expression P points to an element of an array object and the expression Q points to the last element of the same array object, the pointer expression Q+1 compares greater than P. In all other cases, the behavior is undefined.