void *array

hello this is my function:

int deleteItems (void *array, int itemSize, int itemsCount, bool (*shouldDeleteItem)(const void*))
{
for(int i=0; i<itemsCount; i++)
{
if(shouldDeleteItem(array+i*itemSize))
{
itemsCount--;
for(int j=i; j<itemsCount; j++)
*(array+j*itemSize) = *(array+(j+1)*itemSize);
i--;
}
}
return itemsCount*itemSize;
}

compiler says "void*: unknown size", in 5th line.
I want to iterate the void array, how i can do it? thanks
This seems like you're going through too much trouble, can we see more of you code?

EDIT: Is it possible for example to pass the variables in your array to this function recursivley instead of using a function like this?
Last edited on
array+i*itemSize

This will not work; adding an integer to a pointer is pointer arithmetic, and the compiler cannot carry out pointer arithmetic if the size of the object pointed to is unknown. If you want to do this pointer arithmetic, you cannot have a void array.

I would suggest you drop arrays and move to a proper C++ container type.
Last edited on
#include "stdafx.h"
#include <iostream>
using namespace std;


bool isOdd(const void* item)
{
return (*(int*)item)%2;
}


int deleteItems (void *array, int itemSize, int itemsCount, bool (*shouldDeleteItem)(const void*))
{


for(int i=0; i<itemsCount; i++)
{
if(shouldDeleteItem(array+i*itemSize))
{
itemsCount--;
for(int j=i; j<itemsCount; j++)
*(array+j*itemSize) = *(array+(j+1)*itemSize);
i--;
}
}
return itemsCount*itemSize;
}

*/
void main()
{
int arraySize = 0;
int *p = NULL;
int n;
cout<< "Enter length of the array: "<< endl;
cin>> n;
p = new int [n];

cout<< "Numbers in the array: "<< endl;
for(int i=0; i<n; i++)
cin>> *(p+i);

arraySize = deleteItems(p, sizeof(int), n, isOdd);



cout<<p[0]<<" "<<p[1]<<" "<<p[2]<<" "<<p[3]<<" "<<arraySize;

}


this is the whole program
i must use this prototype int deleteItems (void *array, int itemSize, int itemsCount, bool (*shouldDeleteItem)(const void*))
Cast the void* to char* before adding to it.
This CAN'T be the whole code, I don't see your function shouldDeleteItem(...) declared anywhere in this, by the way you don't need to call it twice like that.

Why are you decrementing itemsCount AND incrementing 'i' and comparing them through every intereation?

I'm sure I'll come up with a few more questions.

EDIT: This:
1
2
3
4
5
6
7
8
9
for(int i=0; i<itemsCount; i++)
{
if(shouldDeleteItem(array+i*itemSize))
{
itemsCount--;
for(int j=i; j<itemsCount; j++)// <- A. RIGHT HERE
*(array+j*itemSize) = *(array+(j+1)*itemSize); //<- See B
i--;
//... More Code 


A.)Will work but it is awkward to read, please don't do stuff like that.

B.) This is an issue, what are you trying to accomplish with this line of code?
Last edited on
I want to fill the gaps in the array. shouldDeleteItem(..) is pointer to function isOdd. The real question here is how i can pass to the elements of void array
You already ARE passing the elements of void array. You dynamically allocate them in your Main function (which should not be void by the way) and you pass the pointer on to your deleteItems(...) fucntion just fine. What you need to do is cast them to real types like rocketboy9000 said. Something like this:

int arr = (int)array;
Within your function should do it.
See Here: http://www.cplusplus.com/doc/tutorial/typecasting/

There are other issues that will prevent this from compiling and I was trying to get ahead of those for you with my questions. And yeah I missed the part where shouldDeleteItem(...) is a pointer, that's an interesting way of doing it.
Last edited on
Actually, he should cast it to char* because otherwise his pointer arithmetic won't work properly.
i want to travel over void array not to pass sry wrong word
Yeah I was playing around with his code on my own and I tried int first, so that's what got copy pasted. You clearly state what he should so so hopefully he caught that.

EDIT: @ OP you mean you want to intentionally overrun the void array? or do you mean you want to skip it?
Last edited on
if i cast it to char this function will be only for chars but i want to be for all the types
Cast it to a char* inside the function, not before calling the function. Then it will still be for all types.
Last edited on
Topic archived. No new replies allowed.