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;
}

Recommended Answers

All 3 Replies

I do not recommend using pointer notation for indexing arrays if you do not have to. Array notation is easier to get right. Here is a sample you can use to compare with your code. Yours is very close:

#include <stdio.h>
#include <stdlib.h>

int Resize(int** list, size_t sz)
{
    int rc = 0;

    if (list)
    {
        int* tmp = realloc(*list, sz * sizeof(int));

        if (tmp)
        {
            *list = tmp;
            rc = 1;
        }
    }

    return rc;
}

int Delete(int** list, size_t* sz, size_t pos)
{
    int *data = *list;
    int x = pos;

    for (; pos < *sz-1; ++pos)
    {
        data[pos] = data[pos+1];
    }

    return Resize(list, --*sz);
}

void Populate(int* list, size_t sz);
void Display(int const* list, size_t sz);

int main()
{
    int* list = NULL;
    size_t sz = 10;

    if (Resize(&list, sz))
    {
        Populate(list, sz);
        Display(list, sz);
        Delete(&list, &sz, 3);
        Display(list, sz);
    }

    free(list);

    return 0;
}

void Populate(int* list, size_t sz)
{
    size_t x;

    for (x = 0; x < sz; ++x) list[x] = x;
}

void Display(int const* list, size_t sz)
{
    size_t x;

    for (x = 0; x < sz; ++x) printf("%-5d", list[x]);

    puts("");
}

I do not recommend using pointer notation for indexing arrays if you do not have to. Array notation is easier to get right. Here is a sample you can use to compare with your code. Yours is very close:

#include <stdio.h>
#include <stdlib.h>

int Resize(int** list, size_t sz)
{
    int rc = 0;

    if (list)
    {
        int* tmp = realloc(*list, sz * sizeof(int));

        if (tmp)
        {
            *list = tmp;
            rc = 1;
        }
    }

    return rc;
}

int Delete(int** list, size_t* sz, size_t pos)
{
    int *data = *list;
    int x = pos;

    for (; pos < *sz-1; ++pos)
    {
        data[pos] = data[pos+1];
    }

    return Resize(list, --*sz);
}

void Populate(int* list, size_t sz);
void Display(int const* list, size_t sz);

int main()
{
    int* list = NULL;
    size_t sz = 10;

    if (Resize(&list, sz))
    {
        Populate(list, sz);
        Display(list, sz);
        Delete(&list, &sz, 3);
        Display(list, sz);
    }

    free(list);

    return 0;
}

void Populate(int* list, size_t sz)
{
    size_t x;

    for (x = 0; x < sz; ++x) list[x] = x;
}

void Display(int const* list, size_t sz)
{
    size_t x;

    for (x = 0; x < sz; ++x) printf("%-5d", list[x]);

    puts("");
}

It's not working..line 29 is giving me a hard time..it looks so right and simple..but there's some problem there..it's not getting the address to the next struct..

let's simplify the question how do you put an address of a struct into a pointer that had another's struct address?

Member Avatar for manutm
struct1 *p1;
struct2 *p2;
p2 = (struct2 *)p1;

does this answer your question?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.