void *array

theimmortalbg's Avatar, Join Date: Jan 2011
Newbie Member
hello this is my function:

Code:
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

Last edited by shabbir; 24Jan2011 at 09:45.. Reason: Code blocks
poornaMoksha's Avatar, Join Date: Jan 2011
Ambitious contributor
Well, the compiler needs to know the type of address stored in 'array' pointer....Seems like the function 'deleteItems()' is design in a way that it can accept base address of any kind of array but to use this address one has to tell the compiler the type of elements stored in array ie whether its a charachter array, integer array etc....This is because whenever u add something to 'array'...like 'array+j' then compiler decides through the type of 'array' about how much bytes to jump. For example if 'array' is int* and holds address 1001 and j = 2 then 'array+j' will give us 1009 (assuming integer to be 4 bytes, result is 1001 + (4*2)). Similarly if 'array' is char* and holds address 1001 and j = 2 then 'array+j' will give us 1003 since charachter is of 1 byte and result is calculated as 1001 + (1*2).

Hope this makes u understand the problem and helps u to solve it

Last edited by poornaMoksha; 31Jan2011 at 12:01..
shabbir like this
pein87's Avatar
Ambitious contributor
Your trying to use void which is reserved for functions that dont return something as a type. Generally theres int,float,double,char, and string. Now you can also use unsigned int etc... but you cannot use void as a variable type because void is used for functions the dont have a return value.
shabbir like this