0

so i have these structures, this way my infos are stored

typedef struct date {
      int day, month, year;
} date;

typedef struct director_info {
      char *director_surname, *director_name;
} director_info;

typedef struct movie {
  int id;
  char *title;
  director_info *director;
  date *release_date;
} movie;

And i have this function in order to add new infos but the changes made in the void wont change in the main structures... any ideas?

void addMovie(movie *movies, int n)
{   
    
    int len;
    char tmptitle[MAXN],tmpsur[MAXN],tmpname[MAXN];
    date tmpdt ={.day=0};
    date *newdate;
    char stringToWrite[80];

    movies[n].id=movies[n-1].id+1;// dinw id sthn kainouria ekxwrish
    
    movies = (movie *) malloc(sizeof(movie));
    
    printf("Enter title:"); 
    scanf("%s", &tmptitle); 
    len=strlen(tmptitle);
    movies[n].title = malloc (len + 1);
    memcpy (movies[n].title,tmptitle, len + 1);
    
    director_info *newinfo;   
    //newinfo = malloc (sizeof *newinfo);  
    newinfo = (director_info*) malloc(sizeof(director_info));
    newdate = (date*) malloc(sizeof(date));
    
    printf("Enter the surname of the director:");
    scanf("%s", &tmpsur);
    len = strlen (tmpsur);   
    newinfo->director_surname = malloc (len + 1);
    memcpy (newinfo->director_surname, tmpsur,len + 1);
    memcpy (movies[n].director->director_surname,tmpsur, len + 1);
    
    printf("Enter the name of the director:");
    scanf("%s", &tmpname);
    len= strlen (tmpname);
    free(newinfo);
    newinfo->director_name = malloc(len + 1);
    memcpy (newinfo->director_name,tmpname, len + 1);
    memcpy (movies[n].director->director_name,tmpname,len + 1); 
   // movies[n].director = newinfo;
   // printf("%s",movies[n].director);   
   
    printf("Enter the year of release_date:year/month/day \n");
    scanf("%d%d%d", &tmpdt.year,&tmpdt.month,&tmpdt.day);
     
      
             printf("done\n");
            newdate->day = tmpdt.day;       /* populate date struct from tmpdt struct */
            newdate->month = tmpdt.month;
            newdate->year = tmpdt.year;
  
            //len=sizeof(newdate);
          //  movies[n].release_date = malloc(len + 1);
            movies[n].release_date = newdate; 

  
}

i thought with mempcy there would be a direct allocation but when i try to print the entries in the main function, everything i typed everything is null or 0

  • Does this answer your question? [Changing address contained by pointer using function](https://stackoverflow.com/questions/13431108/changing-address-contained-by-pointer-using-function) – kaylum Nov 18 '20 at 20:28
  • The candidate duplicate post above is in reference to this line: `movies = (movie *) malloc(sizeof(movie));`. As you have found, that doesn't change the caller's pointer value. The post explains why and what can be done to make it work. – kaylum Nov 18 '20 at 20:29
  • `movies[n].id=movies[n-1].id+1; movies = (movie *) malloc(sizeof(movie));`. That also looks wrong. The `malloc` will allocate new memory and the contents of the previous `movies` array will be lost (and a memory leak occurs). You may want to look up `realloc` if you are intending to increase the size of the array. – kaylum Nov 18 '20 at 20:33

1 Answers1

0

Aren't you loosing your graps on movies ? When doing

 movies = (movie *) malloc(sizeof(movie));

In my opinion you are deleting the local copy you had of whatever data you passed as argument from this line

Elley
  • 720
  • 8
  • 16