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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T04:05:28+00:00 2026-05-11T04:05:28+00:00

When compiling the following code: void DoSomething(int Numbers[]) { int SomeArray[] = Numbers; }

  • 0

When compiling the following code:

void DoSomething(int Numbers[]) {     int SomeArray[] = Numbers; } 

the VS2005 compiler complains with the error C2440: ‘initializing’ : cannot convert from ‘int []’ to ‘int []’

I understand that really it’s trying to cast a pointer to an array which is not going to work. But how do you explain the error to someone learning C++?

  • 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. 2026-05-11T04:05:29+00:00Added an answer on May 11, 2026 at 4:05 am

    There are three things you need to explain to the person you’re trying to help:

    1. Arrays can’t be passed by value to a function in C++. To do what you are trying to do, you need to pass the address of the start of the array to DoSomething(), as well as the size of the array in a separate int (well, size_t, but I wouldn’t bother saying that) argument. You can get the address of the start of some array myArray with the expression &(myArray[0]). Since this is such a common thing to want to do, C++ lets you use just the name of the array — e.g. myArray — to get the address of its first element. (Which can be helpful or confusing, depending on which way you look at it.) To make things even more confusing, C++ allows you to specify an array type (e.g. int Numbers[]) as a parameter to a function, but secretly it treats that parameter as though it was a declared as a pointer (int *Numbers in this case) — you can even do Numbers += 5 inside DoSomething() to make it point to an array starting at the sixth position!

    2. When you declare an array variable such as SomeArray in C++, you must either provide an explicit size or an ‘initialiser list’, which is a comma-separated list of values between braces. It’s not possible for the compiler to infer the size of the array based on another array that you are trying to initialise it with, because…

    3. You can’t copy one array into another, or initialise one array from another in C++. So even if the parameter Numbers was really an array (say of size 1000) and not a pointer, and you specified the size of SomeArray (again as say 1000), the line int SomeArray[1000] = Numbers; would be illegal.


    To do what you want to do in DoSomething(), first ask yourself:

    1. Do I need to change any of the values in Numbers?
    2. If so, do I want to prevent the caller from seeing those changes?

    If the answer to either question is ‘No’, you don’t in fact need to make a copy of Numbers in the first place — just use it as is, and forget about making a separate SomeArray array.

    If the answer to both questions is ‘Yes’, you will need to make a copy of Numbers in SomeArray and work on that instead. In this case, you should really make SomeArray a C++ vector<int> instead of another array, as this really simplifies things. (Explain the benefits of vectors over manual dynamic memory allocation, including the facts that they can be initialised from other arrays or vectors, and they will call element constructors when necessary, unlike a C-style memcpy().)

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

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Probably because the virtual machine implementation differs between 32/64 bit… May 11, 2026 at 11:34 pm
  • Editorial Team
    Editorial Team added an answer Your selector is looking for any descendants of a checkbox… May 11, 2026 at 11:34 pm
  • Editorial Team
    Editorial Team added an answer If these are all code you created, the best way… May 11, 2026 at 11:34 pm

Related Questions

Can anyone confirm what the standard says about the default return type of vararg
I am attempting to write a dynamic dispatcher for a function that's templated on
I have an application where I need to write a new getpid function to
I was doing some performance metrics and I ran into something that seems quite

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.