Urgent! Please! How to use void pointer to generate 2D Array?

Hi guys! I'm absolutely no idea and frustrating due to I really cannot figure the way to generate a 2D array by using void pointer.

For example, I was given void***m(m is a void* type to generate 2d array)

For an integer pointer, I'll do as follow: How about void* pointer?

Many thanks and I hope someone can assist me. Thank you very much!

1
2
3
4
5
6
7
8
9
10
11
12
13

int **m;
int row = 2;
int col = 2;

m = new int* [row];
	
for(int i=0; i< row; i++)
   m[i] = new int[col];

for (int i = 0; i < row; i++)
   for (int j = 0; j < col; j++)
      m[i][j] = rand () % 10;

1
2
3
4
5
6
7
8
void*** m;
int row = 2;
int col = 2;

m = new void** [row];
	
for(int i=0; i< row; i++)
   m[i] = new void*[col];
@hamsterman, thanks for your help!

However, how can i fill in the 2D array?

Can I do as below? Or do you have any better suggestion?
1
2
3
4
5
6
7
8
9
10
11
12
	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;
		}
	}		

Last edited on
That's right.

I don't see why would you want to make such array at all, though.
To access an element you'll have to write *((int*)m[i][j]), instead of m[i][j]...
Last edited on
I see! Just a homework to be familiar with void pointer.

Besides, what is the difference between

*((int*)m[i][j]) and int item = *(static_cast<int *>(m[i][j]));?


*((int*) casts the void pointer to an integer pointer, because it's an error to dereference a void pointer directly.
I was taught by using this int item = *(static_cast<int *>(m[i][j]));? in Uni. What is the difference?
closed account (1yR4jE8b)
static_cast<> is the proper C++ typecasts. The other way are old-style C typecasts and should be avoided in new code.

http://cplusplus.com/doc/tutorial/typecasting/

C++ offers much more powerfull and flexible casting functions, it's definitely beneficial to learn them as opposed to old style C casts.
Last edited on
Or you could just make 'm' an int pointer and then you don't have to cast. (Not to mention it makes more sense, is more clear in your code, and less error prone).

Why on Earth do you want to use void pointers here?

EDIT:

oh wait...
ust a homework to be familiar with void pointer.


*cringes*

Man... I really have to wonder wtf is up with some of these C++ courses.
Last edited on
@ darkestfright , hamsterman, Disch

I appreciate your help guys, many thanks !

@Disch

I know it is more complex while using void* pointers. However, according to my prof, the main reason of using it is because the data type can be more general. In fact, you can choose not to use it. haha...
Last edited on
closed account (1yR4jE8b)
Unless you want the container to have non-homogeneous types, there is no reason to use a void* instead of templates. Your prof sounds like an old C hippie who has to teach a C++ course, because void* were very common in C because you didn't have templates but now their practical uses are very few and far between (in C++ programs).
Topic archived. No new replies allowed.