I need to pass a 2D array of int's into a function, which is normally fine, but the issue is that I don't know the last depth of the array. I know how to do it if I knew the length of the last dimension, but it is user defined. How can I do it? Also, I can't use the vector class :( Thanks in advance

6 Years
Discussion Span
Last Post by mattloto

Since the array is user defined you must be declaring the array using dynamic memory and storing the user input as variables. Try passing the array as type int** in addition to the variables containing the user's input.


Thanks, but I'm not sure how to get it to work. I have a variable, n, which is both the length and width of the array. I'm pulling information from the file, which I need to put into the array (probably in nested for loops). Then I need to pass the 2D array into a function that will rotate the array and return the rotated 2D array. I have the rotation function working, but I don't know how to initialize the 2D array, pass it as a function, and return a different array. I need code for it because I don't understand the syntax for arrays very well.


Well if you showed us how you declared and initialised your array, then we could tell you how to pass it to a function.

Hint: just copy/paste the original declaration into your function prototype.


Here is my declaration:

int** b;
	b = new int*[n];
	for (int row = 0; row < n; row++)
		   b[row] = new int[n];
		   for (int col=0;col<n;col++)

I would just like to test this out with them all being 0. I could add in my code to read from the file but I want to just get this working, since I can do the rest by myself. Also, thanks all for helping with this

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.