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

  • Home
  • SEARCH
  • 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 6623201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:30:18+00:00 2026-05-25T21:30:18+00:00

yet another beginner-to-intermediate question. I’m trying to pass a 2-D array to a function

  • 0

yet another beginner-to-intermediate question. I’m trying to pass a 2-D array to a function in C++. I’m aware that the array can’t be sent directly to the function, so I first created a pointer (names edited but you’ll get the idea):

double input[a][b] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
Class.calculate (*(input), a, b);

Then I try to use it in said function – but seemingly I’m unaware on how to dereference the pointer to be able to handle the 2-D array again. My code (more or less):

for (int x=0; x<=a; x++){
    for (int y=0; y<=b; y++){
        tmpInput[x][y]= (*input)[x][y];
    }
}

The compiler complains about an error, namely invalid types ‘double[int]’ for array subscript, but I still can’t figure out the problem. My best bet is that I didn’t dereference the 2-D array properly, but the other option is that C++ can’t dereference 2-D arrays directly, instead relying in converting the array to 1-D before sending it. Any ideas?

  • 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-25T21:30:18+00:00Added an answer on May 25, 2026 at 9:30 pm

    It’s hard to say for sure without seeing the declaration of calculate(), but I’d guess it’s an argument type mismatch. You can send your array as a double*, but not as a double** – it is an array of doubles (they’re laid out linearly in memory, even if the compiler lets you treat them as a multidimensional array). But it is not an array of pointers to doubles, which is what the double** type actually means: Dereference a double** and you get a double*.

    If you know that the second subscript is constant (in your case, 2), you can define calculate() to expect a N*2 array of doubles:

    void calculate(double array[][2], int a)
    {
        //Whatever
    }
    

    Or, if both dimensions can change, just take a straight double* and to the pointer math yourself (you’ll probably have to cast your array to a double* when calling this, but it’ll work):

    void calculate(double* array, int a, int b)
    {
        //To get array[i][j]:
        array[i * b + j];
    }
    

    Hope this helps!

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

Sidebar

Related Questions

yet another basic question ... I have an app that takes about 4-5 secs
Yet another simple question about unit testing model objects that uses data access layer.
Yet another list-comparing question. List<MyType> list1; List<MyType> list2; I need to check that they
This is yet another Unit Testing question. I'm trying to draw knowledge from a
Yet another bandwidth related question. I expected the plots of Device-to-host bandwidth and that
I've just added yet another 3rd-party component to my .net project that contains a
I guess this question will sound familiar, but I am yet another programmer baffled
yet I didnt find a jquery function that validate value to be a future
This is yet another MySQL literature question. I asked for some MySQL / DBA
Yet another IE is doing something different from other browsers question but this is

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.