سؤال

typedef struct dvdtype{
   int dvdcode;
   char title[50];
   int customerID;
   int daysowned;
}dvdtype;

typedef struct dvdstruct{
    dvdtype *dvd;
    int numdvds;
}dvdstruct;

void initDvds(dvdstruct *dvds);
int displayMainMenu();
void insertMovie(dvdstruct *dvds,int *n);

void initDvds(dvdstruct *dvds)
{
    int i;

    dvds->dvd=(dvdtype*)malloc(5*sizeof(dvdtype));
    if(dvds->dvd==NULL){

        printf("not enough memory\n");
        exit(1);
    }

    dvds->numdvds=0;
    for(i=0;i<5;i++)
    {
        dvds->dvd[i].customerID=-1;
        dvds->dvd[i].daysowned=-1;
        dvds->dvd[i].dvdcode=-1;
        dvds->dvd[i].title[0]='\0';
    }
}

void insertMovie(dvdstruct *dvds,int *n){
    int code;

    if(dvds->numdvds>=(*n))
    {
        dvds->numdvds++;
        dvds->dvd=realloc(dvds->dvd,(dvds->numdvds)*sizeof(dvdtype));
        if(dvds->dvd==NULL){

            printf("not enough memory\n");
            exit(1);
        }

        printf(" realloc succesful,size now is :%d \n",dvds->numdvds);
        dvds->dvd[dvds->numdvds].customerID=-1;
        dvds->dvd[dvds->numdvds].daysowned=-1;
        printf("give code and name of movie \n");
        scanf("%d\n",&code);
        dvds->dvd[dvds->numdvds].dvdcode=code;
        gets(dvds->dvd[dvds->numdvds].title);
    }
    else
    {
        printf("give code and name of movie \n");
        scanf("%d\n",&code);
        dvds->dvd[dvds->numdvds].dvdcode=code;
        gets(dvds->dvd[dvds->numdvds].title);
        dvds->numdvds++;
    }
}

I wrote this program to make a list of movies. With the function inidvds I "make room" with malloc for 5 dvds and initialize. Then, the user calls the function insertmovie to insert movie to the file.

The teacher wants us the following. If the user gives 5 dvds (that's the size that we wanted from the start), to call the function realloc and make room for 1 more dvd. This works, I mean when I give 5 movies it's okay. I have a choice for print movies and it prints all 5 movies. Then if I choose to add movie again (n=5) it goes to the if and must do the realloc function.

I put this

printf(" malloc successful,size now is :%d \n",dvds->numdvds);

so I can see if it does go to realloc. It prints:

malloc is successful and the new size is 6

and it asks from me to give code of movie and name which is what we want. I give a code and the name, then I print the list again. It prints the 5 first movies that I have given ok but it doesn't print the 6th movie.

For example:

Dvdcode is 5
dvd name is: tarzan
Customer id is: -1(the initialized value)

let's say that's the fifth movie that I had given. Then the 6th is like this.

Dvdcode is 0
dvd name is:   
Customer id is: 0
(Dvd name is empty)

What does that mean? realloc doesn't work?

هل كانت مفيدة؟

المحلول

Arrays in C are 0-based, that is the first element is array[0] the second is array[1] and so on.

So those lines of insertMovie():

  if(dvds->numdvds>=(*n))
  {
    dvds->numdvds++;
    dvds->dvd=realloc(dvds->dvd,(dvds->numdvds)*sizeof(dvdtype));
    if(dvds->dvd==NULL){

        printf("not enough memory\n");
        exit(1);
    }

    printf(" realloc succesful,size now is :%d \n",dvds->numdvds);
    dvds->dvd[dvds->numdvds].customerID=-1;
    dvds->dvd[dvds->numdvds].daysowned=-1;
    printf("give code and name of movie \n");
    scanf("%d\n",&code);
    dvds->dvd[dvds->numdvds].dvdcode=code;
    gets(dvds->dvd[dvds->numdvds].title);

shall be:

  if(dvds->numdvds>=(*n))
  {
    dvds->dvd=realloc(dvds->dvd,(dvds->numdvds + 1)*sizeof(dvdtype));
    if(dvds->dvd==NULL){

        printf("not enough memory\n");
        exit(1);
    }

    printf(" realloc succesful,size now is :%d \n",dvds->numdvds + 1);
    dvds->dvd[dvds->numdvds].customerID=-1;
    dvds->dvd[dvds->numdvds].daysowned=-1;
    printf("give code and name of movie \n");
    scanf("%d\n",&code);
    dvds->dvd[dvds->numdvds].dvdcode=code;
    gets(dvds->dvd[dvds->numdvds].title);

    dvds->numdvds++;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top