Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 我想在void函数的结构数组中添加信息,但我想我不';不要正确使用指针_Arrays_C_Pointers_Dynamic_Structure - Fatal编程技术网

Arrays 我想在void函数的结构数组中添加信息,但我想我不';不要正确使用指针

Arrays 我想在void函数的结构数组中添加信息,但我想我不';不要正确使用指针,arrays,c,pointers,dynamic,structure,Arrays,C,Pointers,Dynamic,Structure,所以我有这些结构,这样我的信息就可以存储了 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;

所以我有这些结构,这样我的信息就可以存储了

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;
我有这个功能是为了添加新的信息,但是在虚空中所做的改变不会改变主要结构。。。有什么想法吗

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; 

  
}

我原以为使用mempcy会有一个直接分配,但当我尝试打印主函数中的条目时,我键入的所有内容都是null或0,你不是在电影中丢失了图形吗?当做

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

在我看来,您正在删除您从此行作为参数传递的任何数据的本地副本

这是否回答了您的问题?上面的候选重复帖子引用了这一行:
movies=(movie*)malloc(sizeof(movie))。正如您所发现的,这不会改变调用方的指针值。这篇文章解释了为什么以及可以做些什么来让它工作。
movies[n].id=movies[n-1].id+1;电影=(电影*)malloc(电影大小)。这看起来也是错误的。
malloc
将分配新内存,以前的
movies
数组的内容将丢失(并发生内存泄漏)。如果要增加数组的大小,您可能需要查找
realloc