简体   繁体   English

我想在来自 void 函数的结构数组中添加信息,但我认为我没有正确使用指针

[英]I want to add info in a array of structures from a void function but i think i don't use the pointers right

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我认为使用 mempcy 会直接分配,但是当我尝试打印主函数中的条目时,我输入的所有内容都是空或 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在我看来,您正在删除您从这一行作为参数传递的任何数据的本地副本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 c 语言的字符串数组中提取单词并将其存储在另一个一维数组中? 我不想使用指针 - How to extract words from a string array in c language and store it in another 1-d array? I don't want to use pointers 我想显示来自 react 的数组列表,但是当我认为我做的一切都正确时它没有显示? - i want to display list of an array from react but it's not showing when i think i did everything right? 为什么我不必使用指针? - Why don't I have to use pointers? 我不认为我的数组正在存储和数据 - I don't think that my array is storing and data Array.sort函数影响我认为不应该影响的数组 - Array.sort function affecting arrays that I don't think it should affect 我想我需要一个数组,但我不知道如何构建它 - I think I need an array, but I don't know how to build this 我想在 postgress 中插入一个数组类型的数组,但我不知道如何从节点执行它 - I want to insert an array of type array into postgress but I don't know how to do it from node 在python中,我想创建一个新数组并添加另一个数组的元素。 我不知道我哪里出错了 - In python,i want to make a new array and add elements of another array. I don't know where i'm making mistakes 我想使用数组函数来转换数组 - I want to use array function to convert array 我认为我没有正确地破坏动态内存 - I don't think I am destructing dynamic memory properly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM