// -----------------
// StackArrays2D.c++
// -----------------
#include <algorithm> // count, fill
#include <cassert> // assert
#include <cstddef> // size_t
#include <iostream> // cout, endl
#include <vector> // vector
const std::size_t r = 5;
const std::size_t c = 10;
const std::size_t s = r * c;
const int v = 2;
typedef int (*pointer2d) [c];
void f (int p[][c]) {
assert(sizeof(p) == 4);
++p[2][3];
++(*((*(p + 2)) + 3));}
void g (pointer2d p) {
assert(sizeof(p) == 4);
++p[2][3];
++(*((*(p + 2)) + 3));}
void h (int* p) {
assert(sizeof(p) == 4);
++p[(2 * c) + 3];
++*(p + 23);}
void x (int** p) {
assert(sizeof(p) == 4);
++p[2][3];
++(*((*(p + 2)) + 3));}
int main () {
using namespace std;
cout << "StackArrays2D.c++" << endl;
{
int a[r][c] = {{2, 3}, {4, 5, 6}, {7, 8, 9, 10}};
assert(a[2][3] == 10);
for (size_t i = 0; i != r; ++i) {
fill(a[i], a[i] + c, v);
assert(count(a[i], a[i] + c, v) == c);} // warning
assert(a[2][3] == v);
// const int* p = a; // doesn't compile
const pointer2d p = a;
assert(&a[2][3] == &p[2][3]);
assert(sizeof(a) != sizeof(p));
assert(sizeof(a) == r * c * sizeof(int));
assert(sizeof(p) == 4);
f(p);
assert(a[2][3] == v + 2);
g(p);
assert(a[2][3] == v + 4);
// h(p); // doesn't compile
// x(p); // doesn't compile
// const pointer2d q = a[0]; // doesn't compile
int* const q = a[0];
assert(&a[2][3] == &q[(2 * c) + 3]);
// f(q); // doesn't compile
// g(q); // doesn't compile
h(q);
assert(a[2][3] == v + 6);
// x(q); // doesn't compile
fill(q, q + s, v);
assert(a[2][3] == v);
}
cout << "Done." << endl;
return 0;}
syntax highlighted by Code2HTML, v. 0.9.1