-5

When we assign a pointer to a variable we are tend to use the '&' operator as in : int a = 10; int *aa = &a;

If not used it gives error. But I came across this code where pointer is assigned to array arr but '&' isn't used

    int arr[10];
    int *a = &arr;

and on using it shows the error: cannot convert 'int ()[10]' to 'int' in initialization.

Amol
  • 3
  • 3

1 Answers1

1

As the link @πάντα ῥεῖ shared will tell you, C style arrays decay into a pointer to the first element in the array, so when sending such an array to any function, it needs to be accompanied with the size of the array or else the function will not now its size (unless provided by other means). The streaming operators, operator<< and operator>> are just functions that take one argument, so

std::cin >> arr;

will try to find a function with the signature:

std::istream& operator>>(std::istream&, int*);
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108