1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

void *array

Discussion in 'C' started by theimmortalbg, Jan 23, 2011.

  1. theimmortalbg

    theimmortalbg New Member

    hello this is my function:

    int deleteItems (void *array, int itemSize, int itemsCount, bool (*shouldDeleteItem)(const void*))
    	for(int i=0; i<itemsCount; i++)
    			for(int j=i; j<itemsCount; j++)
    				*(array+j*itemSize) = *(array+(j+1)*itemSize);
    	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 a moderator: Jan 24, 2011
  2. poornaMoksha

    poornaMoksha New Member

    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: Jan 31, 2011
    shabbir likes this.
  3. pein87

    pein87 New Member

    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 likes this.

Share This Page