3

The syntax of function in C++ is:

return_type function_name(parameter1, parameter2, ...)
{
    // function body
}

I have some doubts regarding "returning reference of an array from a function":

  1. Return type is specified first then function name but to return reference of an array it is written as:

    // for array of size 10
    int (&f())[10] {
       return global;
    }        
    

    why?

  2. Can it be written as below:

    // return type followed by function name
    int&[10] f(){
       return global;
    }
    

    or something similar(without typedef)?

  3. I couldn't understand typedef usage when using it like:

    typedef int array_t[10];
    array_t& f() {
       return global;
    }
    

    how will the above code be simplified? and will the simplified code look like the code in point 1?

4
  • 2
    1) why? ? Because the language (standard) specifies it to be that way. What other reason would you expect? 2) No. 3) how will the this code be simplified? Please correct the question, "how will the".. what be simplified? will it look like the code in point 1? Well, obviously the snippets in point 1 and 3 "look" different. And yes, the function declarations in point 1 and 3 are compatible with each other. Commented May 15, 2020 at 13:24
  • 1
    A simpler/clearer way to typedef: using array_t = int[10]; Commented May 15, 2020 at 13:26
  • 1
    "how will the this code be simplified?" - By not using a C-style array, but instead a std::array. Commented May 15, 2020 at 14:11
  • 1
    @KamilCuk, I think when someone asks "why is this feature of the language implemented like this" they expect the rationale why the standard specifies it that way, if any. Commented May 15, 2020 at 16:16

1 Answer 1

3
  1. why?

Because Dennis Ritchie designed it to be so in C, and because C++ was designed to be compatible with C.

There weren't references in C of course, but references follow the same syntax rules as pointers do, which were inherited from C.

The main confusion however doesn't seem to be about where the reference puncutator goes, but rather about the array syntax. The array size simply always goes to the right side of the name. This same pattern is also true for variables:

int arr[size];
|   |  |
|   |  size
|   name
element type

int (&arr_ref)[size];
|     |       |
|     |       size
|     name
element type

int (&f())[10]
|     |   |
|     |   size
|     name
element type

Function declaration is different only in the regard that it has the parameter list. Which is always immediately after the name. And because it is immediately after the name, it is consequently before the array size.

  1. Can it be written as below:

No. That is ill-formed in C++.

  1. I couldn't understand typedef usage when using it like:

typedef is simply an alias; a different name for the type.

how will the this code be simplified?

The code in 3. is optimally simple. Personally, I prefer the using syntax for type aliases instead of typedef:

using array_t = int[10];

Another option is to use trailing return type, although it whether this is more or less simple is subjective:

auto f() -> int(&)[10] {

and will it look like the code in point 1?

The decaration looks different, but it declares the same function.

Sign up to request clarification or add additional context in comments.

3 Comments

references follow the same syntax rules as pointers do, which were inherited from C but in C pointer to array is returned int * f(){ // body of function } unlike the way reference to array is returned by specifying size after function name.
@Shub int * is not a pointer to an array. It is a pointer to an integer. Reference to an integer has same syntax: int & f(). And pointer to an array has same syntax as reference to array: int (*f())[10]
Thanks. Also, could you explain for typedef int array_t[10]; how compiler simplifies array_t& f() to int (&f())[10].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.