Confusing UB warning


the C11 standard provides the following example for variable-length arrays and pointer arithmetics (see §6.5.6:10):

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

When compiling the program with Clang 3.9, I get the following warning:

warning: subtraction of pointers to type ‘int [m]’ of zero size has undefined behavior [-Wpointer-arith]

This is confusing (and probably a bug), since m is not zero and both pointers refer to the same object. If I define a as int a[n][3] the warning no longer appears.

  • Manuel

Adding cfe-dev. Removing llvm-dev.