3 ms·
You're not alone. I've been programming in either C or C++ for 25 years, and it wouldn't have occurred to me that you can have a "pointer to array of size N" th
by psyc 10y ago
You're not alone. I've been programming in either C or C++ for 25 years, and it wouldn't have occurred to me that you can have a "pointer to array of size N" that includes the size. Though I probably could have been led there with a little Socratic questioning.
- jjnoakes 10y agoIf you start thinking about two dimensional arrays, you'll probably get close quickly.
- int_19h 10y agoThe reason why people don't usually run into this is because C tries really hard to decay your arrays to pointers to first element, so there are very few cases where it actually comes up - sizeof(array) and &array are some of the few. On top of that, writing down the type of such an array is not exactly obvious, and requires parentheses: int (*p)[10]; This all is much more interesting in C++, because there, in conjunction with references, this lets you write functions that take arrays as arguments and know their length. Like so: template<size_t N> void foo(const int (&a)[N]) { for (size_t i = 0; i < N; ++i) cout << a[i]; } int a[10]; foo(a);