Q:

Revise the arrPtr function on to return a reference to the array

0

Revise the arrPtr function on to return a reference to the array.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <iostream>

int odd[] = {1, 3, 5, 7, 9};
int even[] = {0, 2, 4, 6, 8};
auto arrPtr(int i) -> int (&)[5] {
  return i % 2 ? odd : even;
}

int main() {
  int i;
  std::cin >> i;
  int (&arr)[5] = arrPtr(i);
  int *arr2 = arrPtr(i);
  //int arr3[5] = arrPtr(i);  // Error
  //int (*arr4)[5] = arrPtr(i);  // Error
  std::cout << arr[0] << " " << arr2[0] << std::endl;
  return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now