Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   void *array (http://www.go4expert.com/forums/void-array-t24700/)

theimmortalbg 23Jan2011 23:09

void *array
 
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

poornaMoksha 31Jan2011 11:59

Re: void *array
 
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

pein87 1Feb2011 08:35

Re: void *array
 
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.


All times are GMT +5.5. The time now is 08:53.