construct 2D array using void pointer

Hi guys.
This time i want to ask something about generate 2D array using void pointer.
I've searched through this forum and i found this topic which is related to my problem.

http://www.cplusplus.com/forum/general/19209/

But. Although i've done the same way as him (the guy who's had the same problem as me in the forum above), the number won't come out.
The thing that came out was something like 'address' of the number.

Here is my code.

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

typedef void* VoidPtr;

class Matrix
{
	public:
		Matrix();			//constructor
		Matrix (int, int);		//number of rows and col
		void printMatrix();

	private:
		int row;
		int col;
		void ***m;
		void initMatrix();
		void generateMatrix();	   	   
};

Matrix::Matrix()
{
	//do nothing
}

Matrix::Matrix (int row, int col)
{
	this -> row = row;
	this -> col = col;
	Matrix::initMatrix();
	Matrix::generateMatrix();
}

void Matrix::printMatrix()
{
	for (int i = 0; i < row; i++)
	{	
		cout << "\t" << "[";
		for (int j = 0; j < col; j++)
		{
			cout << *(*(m + i) + j) << "\t";
		}
		cout << "]" << endl;
	}
}

void Matrix::initMatrix()
{
	m = new void** [row];
	
	for(int i=0; i< row; i++)
	{
		m[i] = new void*[col];
	}
}
void Matrix::generateMatrix()
{
	int*p;
		 	  
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			p = new int;
			*p = rand () % 10;
			*(*(m + i) + j) = p;
		}
	}		
}

int main()
{
	srand(time(NULL));
	int choice;
	int row;
	int col;

	cout << "Enter the number of rows and columns: ";
	cin >> row >> col;
	cout << "Given matrix" << endl << endl;
	Matrix mtx (row, col);	   
	mtx.printMatrix();
	cout << endl;
}	


thanks. :)
Last edited on
m is a void***, so
*(m + i) is a void**,
*(*(m + i) + j) is a void*.
You need int, so you have to cast it to int* and then dereference:
*((int*) *(*(m + i) + j))
closed account (1yR4jE8b)
Wow, this is some real old-school Hippie style C stuff...why not just use templates? It would save you sooooooooo many headaches.
i've changed it into *((int*) *(*(m + i) + j)) and it works.
thanks hamsterman. :)
to darkestfright :
i dunno. i just follow what my teacher order me to do.
it's a student duty. LOL. :)
I find these kinds of things to be very much helped when I use type aliases:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  // Type aliases
  typedef void*      element_t;  // an element is a pointer to something
  typedef element_t* column_t;   // a list of elements is a column
  typedef column_t*  row_t;      // a list of columns is a row

  // Variables
  unsigned number_of_rows;
  unsigned number_of_columns;
  row_t    data;

  ...

  // Initialize
  data = new row_t[ number_of_rows ];

  for (unsigned row = 0; row < number_of_rows; row++)
    {
    data[ row ] = new column_t[ number_of_columns ];

    for (unsigned col = 0; col < number_of_columns; col++)
      {
      data[ row ][ col ] = (element_t)(new int);
      }
    }

Topic archived. No new replies allowed.