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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:35:52+00:00 2026-05-22T16:35:52+00:00

Like the question says, I am trying to pass multi-dimensional arrays into a function

  • 0

Like the question says, I am trying to pass multi-dimensional arrays into a function to print it to a file for an engineering project. The format for which the data is inputted CANNOT be changed, so please don’t suggest I just input it as a different datatype.

This particular function anticipates a two-dimensional array (although I have others with three dimensions after this one), where nothing is known about the size of the array until run-time. I know I must use pointers to point to each row of the array separately, but I have NO idea what the syntax is for passing it to the function. In the following code, the array in question is ‘block’. The main function is just a little testing example I made to try to make it work:

#include<fstream>
using namespace std;

void of_write_blocks(string filename, string block_type[], int **block,
            int grid[][3], string grade_type[], int grade[][3], int n_blocks, int m[])
{
    ofstream file_out(filename.c_str(),ios::app);
    file_out<<"\nblocks\n(\n";

    for(int i=0;i<n_blocks;++i) {
            file_out<<"   "<<block_type[i]<<" ( ";
                    for(int j=0;j<m[i];++j)
                            file_out<<block[i][j]<<" ";
            file_out<<") ( ";
            file_out<<grid[i][0]<<' '<<grid[i][1]<<' '<<grid[i][2]<<" ) ";
            file_out<<grade_type[i]<<" ( ";
            file_out<<grade[i][0]<<' '<<grade[i][1]<<' '<<grade[i][2]<<" )\n";
    }
    file_out<<");\n";
}


//testing example:
int main()
{       
    int block[6][9];
    for(int i=0; i<6;++i) 
            for(int j=0; i<9;++j)
                    block[i][j] = i*j;

    int grid[6][3];
    for(int i=0; i<6;++i)
            for(int j=0; i<3;++j)
                    block[i][j] = i*j;


    int grade[6][3];
    for(int i=0; i<6;++i) 
            for(int j=0; i<3;++j)
                    block[i][j] = i*j;

    string grade_type[6] = {"simpleGrading"};
    string block_type[6] = {"hex"};
    int m[6] = {8};
    int n_blocks = 6;

    of_write_blocks("name",block_type,block,grid,grade_type,grade,n_blocks,m);
}       

any help is appreciated!

  • 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-22T16:35:52+00:00Added an answer on May 22, 2026 at 4:35 pm

    You can’t. Multidimensional arrays are syntactic sugar, and are compiled directly into the code that does manipulations on the array, which is a single memory block. The dimensions are not passed into the function as parameters or anything like that as part of the array, as things are done in e.g. Java or C#.

    If you need the dimensions of the array in your function, you’ll need to just accept a pointer to the first element of the array, and the dimensions, and do the multiplies and adds to get the right index yourself.

    Alternately, use something like a std::vector<std::vector<block>>, which pass the dimensions as part of the object, rather than a built in array.

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

Sidebar

Related Questions

Like the question says, I am trying to capture a click on a textbox
Well the question says it all.I am trying to create a file transfer application
Like the question says, if I have a request for a page on my
Like the title says, I'm trying to find elements of M that exist in
The question says it all, really. I know a (Scala) Monad looks like this:
The question says it all: how to get a remotely hosted image into a
I have a simple question. I'm trying to upload a file to my ftp
This may seem like a simple question however i've spent the last hour trying
The question pretty much says it all. I'm trying to create a submission form
Well as the question title says, I am trying to make a projection criteria

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.