Deleting an element from an array
Does anyone know a more elegant way than this of deleting a given element from an array:
Code:
void deletefrom(int[], int, int);
int main(void)
{
/*stuff*/
int bob, size_of_frank;
int *frank;
frank=(int*)calloc(size_of_frank,sizeof(int));
/*call for element "bob" to be deleted from array "frank"*/
deletefrom(frank,bob,size_of_frank);
/*etc*/
return 0;
}
void deletefrom(int frank[], int bob, int size_of_frank)
{
int i, j=0, *temp;
temp=(int*)calloc(size_of_frank-1,sizeof(int));
for(i=0; i<size_of_frank, i++){
if(frank[i]==bob)
continue; /*Or should this be break?*/
else{
temp[j]=frank[i];
j++
}
}
free(frank);
frank=(int*)calloc(size_of_frank-1,sizeof(int));
for(i=0;i<size_of_frank-1;i++)
frank[i]=temp[i];
return;
}
But something about this gives me the creeps and that is "Once I have free'd frank, is he gone from main forever? Can main see the new frank?"
Help is much appreciated, even if it turns out to be the thread where you explained this to someone else last week (I did look! But I couldn't find anything on the first five pages...)
Matt