Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4092800
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:32:15+00:00 2026-05-20T19:32:15+00:00

We can pass reference of an array to a function like: void f(int (&a)[5]);

  • 0

We can pass reference of an array to a function like:

void f(int (&a)[5]);

int x[5];
f(x);     //okay
int y[6];
f(y);     //error - type of y is not `int (&)[5]`.

Or even better, we can write a function template:

template<size_t N>
void f(int (&a)[N]); //N is size of the array!

int x[5];
f(x);     //okay - N becomes 5
int y[6];
f(y);     //okay - N becomes 6

Now my question is, how to return reference of an array from a function?

I want to return array of folllowing types from a function:

int a[N];
int a[M][N];
int (*a)[N];
int (*a)[M][N];

where M and N is known at compile time!

What are general rules for passing and returning compile-time reference of an array to and from a function? How can we pass reference of an array of type int (*a)[M][N] to a function?

EDIT:

Adam commented : int (*a)[N] is not an array, it’s a pointer to an array.

Yes. But one dimension is known at compile time! How can we pass this information which is known at compile time, to a function?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T19:32:15+00:00Added an answer on May 20, 2026 at 7:32 pm

    If you want to return a reference to an array from a function, the declaration would look like this:

    // an array
    int global[10];
    
    // function returning a reference to an array
    int (&f())[10] {
       return global;
    }
    

    The declaration of a function returning a reference to an array looks the same as the declaration of a variable that is a reference to an array – only that the function name is followed by (), which may contain parameter declarations:

    int (&variable)[1][2];
    int (&functionA())[1][2];
    int (&functionB(int param))[1][2];
    

    Such declarations can be made much clearer by using a typedef:

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

    If you want it to get really confusing, you can declare a function that takes a reference to an array and also returns such a reference:

    template<int N, int M>
    int (&f(int (&param)[M][N]))[M][N] {
       return param;
    }
    

    Pointers to arrays work the same, only that they use * instead of &.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I pass an array of structs by reference in C? As an
Because shells other than ksh do not support pass-by-reference, how can multiple arrays be
If I have a auto_ptr I can pass it for a reference?Like: auto_ptr<MyClass>Class(new MyClass);
I have an array witch I pass to a function by reference to sort
I recently found some code like this: typedef int TenInts[10]; void foo(TenInts &arr); What
In C++ how can I pass an array as a reference when I don't
Can you pass by reference with R ? for example, in the following code:
Checking the documentation online I saw that it is pass by reference. Can someone
You can pass special strings into jQuery's Datepicker class setDate() method like +7 which
I know you can pass arguments through the RunWorkerAsync function call when you first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.