0

How to pass a 2D array by reference? Can somebody guide me?

if I created an array as follows:

char **sarray;
2Darray = new char* [row];
for(i=0; i<row; i++)
2Darray = new char[col];

(NOTE: I get the values of row and colume via some calculations in my code ....basically, I am gathering values from a text file into and placing it in a 2D array... so each text file may result in diff row & colume sizes)

I got the 2D array I need...now I want to pass it by reference to another function. How to do that?

2
Contributors
3
Replies
4
Views
10 Years
Discussion Span
Last Post by John A
0

> How to pass a 2D array by reference?
What's wrong with pointers? Just accept a char ** as the array, and then pass it the 2 array dimensions, so the function knows the boundaries.

0

I don't know how to pass the sizes of the rows & columns actually ....

can you provide me the syntax for passin by reference ? I don't know how to pass it the 2 array dimensions, so the function knows the boundaries.

help

0

>I don't know how to pass the sizes of the rows & columns actually ....
Pass them as integers.

>can you provide me the syntax for passin by reference ?
I'd say that's pretty pointless (no pun intended) to pass by reference, when you've created the array with pointers. So use pointers. Here's how I would do it with pointers:

void myFunction(char **array, int arrayWidth, int arrayHeight) {
This topic has been dead for over six months. 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.