-2
#include<iostream>
using namespace std;
int number(int (&a)[10])
{
    int n= sizeof(a)/sizeof(a[0]);
    return n;
}
int main()
{
    int n;
    cout << " Enter the number of elements ";
    cin >> n;
    int a[10];
    cout << "Enter array elements : ";
    for(int i=0;i<n;i++)
        cin>>a[i];
    cout<<" The number of elements according to main is "<< sizeof(a)/sizeof(a[0])<<endl;
    cout<<"  The number of elements in the function number is " << number(a);
}

In the above code in the main function "number(a)" Here what exactly are we passing and what is taken by the method. and how does the code work in the function.and what is happening if we are not using "&" symbol in the number function.

My understanding is we are passing a pointer to the first element into the function but I dont know properly.

  • 5
    The code is really, really wrong on a few different levels. You should reference a completely different example on whatever it is you're trying to learn. – Saustin Sep 18 '17 at 04:59
  • It's a reference to an array of 10 integers. https://cdecl.org/?q=int+%28%26a%29%5B10%5D You can generalize it using a template: https://stackoverflow.com/questions/2384107/magic-arguments-in-function-templates – Retired Ninja Sep 18 '17 at 05:01
  • @Saustin this is an exam question and we are required to explain about the output in the answer. – shraman shraman Sep 18 '17 at 05:05
  • 4
    That's an exam question? Yikes. You can answer a lot of things and they could all be wrong, because I highly doubt your professor knows what (s)he's doing.. I would HIGHLY recommend viewing different examples online to gain a better understanding than your professor has. – Saustin Sep 18 '17 at 05:08
  • What happen when someone enters 50 for the number of items and writes outside of the allocated memory? Seems like a rather silly exam question. – Retired Ninja Sep 18 '17 at 05:08
  • Possible duplicate of [standard conversions: Array-to-pointer conversion](https://stackoverflow.com/questions/4223617/standard-conversions-array-to-pointer-conversion) – Passer By Sep 18 '17 at 05:14
  • The exam is likely asking about ^that duplicate, but yeah, as mentioned, the code is seriously broken. – Passer By Sep 18 '17 at 05:14
  • @Saustin Could you elaborate on the "really, really wrong on a few different levels" ? It looks correct to me, other than failing to check `n <= 10` before reading – M.M Sep 18 '17 at 05:26
  • If this is an exam question, you will have touched on the difference between `int a[10]` and `int (&a)[10]` parameters during the course. – molbdnilo Sep 18 '17 at 08:09

1 Answers1

0

When you call number(a), you are passing a reference to the array of 10 ints.

Let's take a simpler function.

void foo(int& ref) { }

You can call it with

int i = 20;
foo(i);

Here, a reference to the variable i passed to the function. You can access the value of the variable i in foo through ref. Any changes you make to ref in foo will be visible in the calling function.

Similarly, your function takes a reference to a variable whose type is "an array of 10 ints". The only thing the function does with the reference is compute the number of elements of the array.

It uses a bit of redundant logic. You can simplify that function to:

int number(int (&a)[10])
{
    return 10;
}

The function is limited in what it can work with. It is not going to work if you have an array of type float or if you have an array of 20 elements. You can create a template function to make it more general.

template <typename T, size_t N>
size_t number(T (&arr)[N])
{
   return N;
}

The above function will work with arrays of different object types and sizes.

R Sahu
  • 204,454
  • 14
  • 159
  • 270