Re: Stroustrup 5.9, exercise 10 (using a function)
On 2 Apr, 11:00, "arnuld" <geek.arn...@gmail.com> wrote:
On Apr 2, 1:47 pm, "Erik Wikstr=F6m" <eri...@student.chalmers.se> wrote:
Personally I'd prefer a version where the size is passed as an
argument just to be safe, something like your first try, but also
incrementing arr:
void print_arr(const char** arr, size_t arr_size)
{
std::cout << "\n\tUSING FUNCTION\n";
for(unsigned int i=0; i < arr_size; ++i, arr++)
std::cout << *arr << std::endl;
}
this is the 1st time i have seen someone incrementing an "array",
never saw so even in any book. does it work like this:
*arr == &arr[0]
++arr == &arr[0 + 1]
?
IOW, like a pointer
Yes, arr[N] is the same as *(arr[0] + N) == *(arr + N).
The following might help you understand but should *never* be used in
real life, read the rest at your own risk:
Using the transitivity of addition (meaning that A+B == B+A) we can
get some pretty ugly, but valid, syntax when indexing into arrays:
So arr[N] == *(arr + N) then we apply the transitivity rule on the
left hand giving *(N + arr) and then go back to the array-form gives
N[arr].
So given array arr then 2[arr] will give the third element in arr. And
then we replace the N with a variable and we get something like this:
int main()
{
int arr[3] = {1, 2, 3};
for (int i = 0; i < 3; ++i)
std::cout << i[arr];
return 0;
}
--
Erik Wikstr=F6m