Hey everybody!
I'm having a hard time finding a solution for my problem-
I have a dynamic array that holds structs of Person type..now I want to delete one specific person so I need to move all the ppl after that person one block down in the array and then do realloc (lose the last one) but I guess I move the ppl incorectly because I did a printf before the move and after and I get the same person (that needs to be deleted) please keep in mind I'm a beginer and I couldn't find any example that works in my case..
void main()
{
int* ptrNumOfPpl; //pointer to num of people in array
Person* pPerson=((Person*)malloc(10*sizeof(Person)));
...
...
findPerson(pPerson,ptrNumOfPpl);
}
void findPerson(Person* pPerson, int* ptrNumOfPpl)
{
/*I found the index to the person to delete and put it in the int flg*/
deletePerson(flg,& pPerson,ptrNumOfPpl);
}
int deletePerson(int flg,Person **pPerson, int *ptrNumOfPpl)
{
int i,j;
Person *ptr=NULL;
printf("1. %s\n",(*pPerson+flg)->name); /*prints the name of person that's gonna be deleted*/
for(i=flg;i<*ptrNumOfPpl;i++)
{
*(pPerson+i)=*pPerson+i+1;
}
printf("2. %s\n",(*pPerson+flg)->name); /*prints the name of person that took the place of the deleted person*/
/*unfortunatly I see the same name in both printfs*/
if((ptr=(Person*)realloc(*pPerson,(*ptrNumOfPpl-1)*sizeof(Person)))==NULL)
{
return 0;
}
*pPerson=ptr;
(*ptrNumOfPpl)--;
return 1;
}