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 6330893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:57:13+00:00 2026-05-24T17:57:13+00:00

Why can’t you pass arrays as function arguments? I have been reading this C++

  • 0

Why can’t you pass arrays as function arguments?

I have been reading this C++ book that says ‘you can’t pass arrays as function arguments’, but it never explains why. Also, when I looked it up online I found comments like ‘why would you do that anyway?’ It’s not that I would do it, I just want to know why you can’t.

  • 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-24T17:57:14+00:00Added an answer on May 24, 2026 at 5:57 pm

    Why can’t arrays be passed as function arguments?

    They can:

    void foo(const int (&myArray)[5]) {
       // `myArray` is the original array of five integers
    }
    

    In technical terms, the type of the argument to foo is “reference to array of 5 const ints”; with references, we can pass the actual object around (disclaimer: terminology varies by abstraction level).

    What you can’t do is pass by value, because for historical reasons we shall not copy arrays. Instead, attempting to pass an array by value into a function (or, to pass a copy of an array) leads its name to decay into a pointer. (some resources get this wrong!)


    Array names decay to pointers for pass-by-value

    This means:

    void foo(int* ptr);
    
    int ar[10]; // an array
    foo(ar);    // automatically passing ptr to first element of ar (i.e. &ar[0])
    

    There’s also the hugely misleading “syntactic sugar” that looks like you can pass an array of arbitrary length by value:

    void foo(int ptr[]);
    
    int ar[10]; // an array
    foo(ar);
    

    But, actually, you’re still just passing a pointer (to the first element of ar). foo is the same as it was above!

    Whilst we’re at it, the following function also doesn’t really have the signature that it seems to. Look what happens when we try to call this function without defining it:

    void foo(int ar[5]);
    int main() {
       int ar[5];
       foo(ar);
    }
    
    // error: undefined reference to `func(int*)'
    

    So foo takes int* in fact, not int[5]!

    (Live demo.)


    But you can work-around it!

    You can hack around this by wrapping the array in a struct or class, because the default copy operator will copy the array:

    struct Array_by_val
    {
      int my_array[10];
    };
    
    void func (Array_by_val x) {}
    
    int main() {
       Array_by_val x;
       func(x);
    }
    

    This is somewhat confusing behaviour.


    Or, better, a generic pass-by-reference approach

    In C++, with some template magic, we can make a function both re-usable and able to receive an array:

    template <typename T, size_t N>
    void foo(const T (&myArray)[N]) {
       // `myArray` is the original array of N Ts
    }
    

    But we still can’t pass one by value. Something to remember.


    The future…

    And since C++11 is just over the horizon, and C++0x support is coming along nicely in the mainstream toolchains, you can use the lovely std::array inherited from Boost! I’ll leave researching that as an exercise to the reader.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
Can someone maybe tell me why this is not working? I have used echo
Can I have a project that has some parts written in c and other
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Can somebody point me to a resource that explains how to go about having
can you recommend some good ASP.NET tutorials or a good book? Should I jump
Can a LINQ enabled app run on a machine that only has the .NET
Can I call select before recv_from on a socket that is blocking?
Can anyone suggest how to underline the title of a UIButton ? I have
Can some one confirm me that only one UIWindow instance is possible in any

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.