Passing pointer to 2D array to function

Hey guys.

I am stuck trying to pass a pointer to a 2D array to a function. How is this done semantically?

Thanks in advance.
For example, I want to be able to pass a pointer to a 2D char array to a function something like this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int getStrings(<pointer to a 2D array of character strings>, int maxNumberOfStrings){
	// obtain list of strings from a file
	// place each string into each array element
}

int main(){
	char (*stringArray)[100] = new char[50][100];
	
	int getStrings(<pointer to 2D array of character strings>,50);

	delete [] stringArray;

	return 1;
}
Last edited on
I figured it out pretty much. You can copy/paste/compile the following the check it out if you want:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
using namespace std;
#include <cstdio>
#include <cstring>

void populate2dArray(char (*myArray)[10], int arraySize){
	for(int i = 0; i < arraySize; i++){
		for(int j = 0; j < 10; j++){
			myArray[i][j] = 'a'+ i;
		}
	}
}

int main(){
	printf("\n\n");
	printf("*******************************\n");
	printf("** 2D Array Pointer Test\n");
	printf("*******************************");
	
	int arraySize = 5;
	printf("\n\nNumber of strings to generate = %u.", arraySize);
	
	{	
		char (*myArray)[10] = new char[arraySize][10];
		printf("\n\nPopulating array...");
		populate2dArray(myArray, arraySize);
		printf(" finished.");
		
		printf("\n\nContents of array...");
		for(int i = 0; i < arraySize; i++){
			char c[11]; 
			memcpy(c, myArray[i], 10);
			c[10] = 0;
			printf("\n\t%u: '%s'.", i+1, c);
		}	
		
		delete [] myArray;
	}
	
	printf("\n\n");
	return 1;
}


The main draw back with the above code is that one of the dimensions of the array must be known by the function prototype for "getStrings()'. I couldn't figure out the semantics of using an array with both dimensions determined at run time. Maybe one of you kind folk would be able to enlighten me? :)
I figured out a way to do the same as above using a 2D array where both dimensions can be varied by simply changing the values of xLength and yLength, however the added functionality comes at a price as it is messier than the code above.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
using namespace std;
#include <cstdio>
#include <cstring>

void print2dArray(char **myArray, int xLength, int yLength){
	printf("\n\n2D Array Contents:");
	for(int i = 0; i < xLength; i++){
		char *c = new char[yLength+1];
		memcpy(c, myArray[i], yLength);
		c[yLength] = 0;
		printf("\n\t%u:'%s'", i + 1, c);
		delete [] c;
	}
}

void populate2dArray(char **myArray, int xLength, int yLength){
	for(int i = 0; i < xLength; i++){
			memset(myArray[i], 'a' + i, yLength);
	}
}

int main(){
	printf("\n\n");
	printf("*******************************\n");
	printf("** 2D Array Pointer Test\n");
	printf("*******************************");
	
	int xLength = 5;
	int yLength = 8;
	
	printf("\n\nArray dimensions = (%u, %u).", xLength, yLength);
	
	{	
		char **myArray = new char*[xLength];
		for(int i = 0; i < xLength; i++){
			myArray[i] = new char[yLength];
		}

		populate2dArray	(myArray, xLength, yLength);
		print2dArray	(myArray, xLength, yLength);
		
		for(int i = 0; i < xLength; i++){
			delete [] myArray[i];
		}
		delete [] myArray;
	}
	
	printf("\n\n");
	return 1;
}


I was using Valgrind to test it but it was complaining about mismatched delete/free operators until I used...

 
delete [] arrayToDelete;


...instead of...


 
delete arrayToDelete;


I am not sure why though since I thought you only needed to use 'delete [] arrayToDelete' when using code as follows:

1
2
3
char (*myArray)[50] = new char[arraySize][50];

delete [] myArray;


Ah well I guess I have some reading to do :)

EDIT: I just noticed a redundent loop when mem setting the array. It has been fixed.
Last edited on
Why not just use strings?

Also I know you already solved your problem, but this might be interesting for you anyway:

http://cplusplus.com/forum/articles/17108/
Because ultimately I am hoping to make a generic function that can handle arrays of any type, rather than just strings or character arrays :)

However thank you for the reading titled 'Multidimentional arrays are evil' ^_^

I am going to read it now and see if I agree :P
Last edited on
:D Thanks for the article it was nice and informative. I might try that MD array class at some point later to see how easy I find it to use. I might also try an emulated 2D array using a 1D array.

Also I just noticed it was written by you. Nice article :)
Topic archived. No new replies allowed.