Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I pass multi-dimensional arrays of unknown size into a function using pointers in c++?
    primarykey
    data
    text
    <p>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.</p> <p>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:</p> <pre><code>#include&lt;fstream&gt; 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&lt;&lt;"\nblocks\n(\n"; for(int i=0;i&lt;n_blocks;++i) { file_out&lt;&lt;" "&lt;&lt;block_type[i]&lt;&lt;" ( "; for(int j=0;j&lt;m[i];++j) file_out&lt;&lt;block[i][j]&lt;&lt;" "; file_out&lt;&lt;") ( "; file_out&lt;&lt;grid[i][0]&lt;&lt;' '&lt;&lt;grid[i][1]&lt;&lt;' '&lt;&lt;grid[i][2]&lt;&lt;" ) "; file_out&lt;&lt;grade_type[i]&lt;&lt;" ( "; file_out&lt;&lt;grade[i][0]&lt;&lt;' '&lt;&lt;grade[i][1]&lt;&lt;' '&lt;&lt;grade[i][2]&lt;&lt;" )\n"; } file_out&lt;&lt;");\n"; } //testing example: int main() { int block[6][9]; for(int i=0; i&lt;6;++i) for(int j=0; i&lt;9;++j) block[i][j] = i*j; int grid[6][3]; for(int i=0; i&lt;6;++i) for(int j=0; i&lt;3;++j) block[i][j] = i*j; int grade[6][3]; for(int i=0; i&lt;6;++i) for(int j=0; i&lt;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); } </code></pre> <p>any help is appreciated!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload