'void *' : unknown size

Hello, i'm writing a function which sort a 'void*' array
using index numbers in 'indArr', and return a new, sorted, array.

This suppose to be a generic function that will work with any var. type.

The error i keep getting (on line 7):
1
2
3
error C2036: 'void *' : unknown size
error C2036: 'void *' : unknown size
error C2120: 'void' illegal with all types


the code:
1
2
3
4
5
6
7
8
9
10
void* scramble (void* arr, int ElemSize, int n, int* indArr)
{
	void* newArr = (void*) malloc(ElemSize * n);

	for ( int i=0 ; i < n ; i++ )

		newArr[i] = arr[indArr[i]];

	return newArr;
}


I suppose I'm missing something important here.
Any guidance appreciated.
Last edited on
void pointers don't have any attributes, no size, nothing so you can't do anything with them except pass them around. If you want to use the data they point to then you have to cast them to a data type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void* scramble (void* arr, int ElemSize, int n, int* indArr)
{
	void* newArr = (void*) malloc(ElemSize * n);
        char* new_ptr = reinterpret_cast<char *>(newArr);
        char* orig_ptr = reinterpret_cast<char *>(arr);
        char* orig_idx_ptr;
        int*    idx_ptr;

	for ( int i=0 ; i < n ; i++ )
        {
		// newArr[i] = arr[indArr[i]];
                idx_ptr = indArr + i; // Get next entry from indArr
                orig_idx_ptr = orig_ptr +(*idx_ptr * ElemSize); // find element in arr to copy
                memcpy(new_ptr, orig_idx_ptr, ElemSize); // copy it to the new array
                new_ptr += ElemSize; // move newArr to next element ready for next copy
         }

	return newArr;
}


I've tried to explain with comments but if you don't follow anything post a question

Hope this helps

Bertha
Void is not a data-type.
You use void to declare a function that returns nothing.

so, you have to choose a suitable data-type (int, float, double, char etc.) to create an array.
Then, you can use a function void scramble( ...) with a pointer to that array to do whatever you wish to the array.

int main
bnbertha
Your code doesn't seem to work...

int main
I understand. The whole program looks like this:

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
void* scramble (void* arr, int ElemSize, int n, int* indArr)
{
	void* newArr = (void*) malloc(ElemSize * n);

	for ( int i=0 ; i < n ; i++ )

		newArr[i] = arr[indArr[i]];

	return newArr;
}

void main()
{
	int arr[SIZE] = {100,200,300};  // main array
	//char arr[SIZE] = {'a','b','c'};
	//float arr[SIZE] = {1.5, 5.8, 9.24};

	int n = SIZE;  // array size

	int indArr[SIZE] = {2,0,1};  // index array

	void *p = NULL;

	p = scramble ( (void*)arr, sizeof(arr[0]), n, indArr );

}


So i need this function to work with any data type i throw at it.
Last edited on
If you want to work with any data-type, you should use a template function instead.
It does work, you need p to be an int ptr though to be able to see it.

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
#include <cstdlib>
#include <iostream>

using namespace std;

#define SIZE 3

void* scramble (void* arr, int ElemSize, int n, int* indArr)
{
	void* newArr = (void*) malloc(ElemSize * n);
        char* new_ptr = reinterpret_cast<char *>(newArr);
        char* orig_ptr = reinterpret_cast<char *>(arr);
        char* orig_idx_ptr;
        int*    idx_ptr;

	for ( int i=0 ; i < n ; i++ )
        {
		// newArr[i] = arr[indArr[i]];
                idx_ptr = indArr + i; // Get next entry from indArr
                orig_idx_ptr = orig_ptr +(*idx_ptr * ElemSize); // find element in arr to copy
                memcpy(new_ptr, orig_idx_ptr, ElemSize); // copy it to the new array
                new_ptr += ElemSize; // move newArr to next element ready for next copy
         }

	return newArr;
}

void main()
{
	int arr[SIZE] = {100,200,300};  // main array
	//char arr[SIZE] = {'a','b','c'};
	//float arr[SIZE] = {1.5, 5.8, 9.24};

	int n = SIZE;  // array size

	int indArr[SIZE] = {2,0,1};  // index array

	int *p = NULL;
	int *ptr;

	p = (int *)scramble ( (void*)arr, sizeof(arr[0]), n, indArr );
	ptr = p;
	for (int i = 0; i < SIZE; i++)
	{
		cout << *ptr++ << endl;
	}

}

However, as Zaita pointed out, this is a 'C' solution, the C++ solution would be to use a template
Last edited on
Yeah, I'm writing this in C, thank you!
Topic archived. No new replies allowed.