void *array

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

  1. theimmortalbg

    theimmortalbg New Member

    Joined:
    Jan 23, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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 a moderator: Jan 24, 2011
  2. poornaMoksha

    poornaMoksha New Member

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
    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 Active Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    28
    Occupation:
    Web Dev
    Location:
    Limbo
    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

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice