Guest User

Untitled

a guest
Oct 20th, 2023
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. typedef struct s1_ {
  5. int x,y,z;
  6. } tmp_type;
  7.  
  8. typedef struct s2_ {
  9. tmp_type* p1;
  10. char a;
  11. } tmp_data;
  12.  
  13. typedef struct s3_ {
  14. tmp_data* v1;
  15. int num;
  16. } tmp_list;
  17.  
  18. void get_copy(tmp_list *list) {
  19. tmp_data tmp[10];
  20. int i;
  21. for(i=0;i<10;i++){
  22. tmp[i].p1= calloc(1,sizeof(tmp_type));
  23. tmp[i].p1->x = tmp[i].p1->y = tmp[i].p1->z = 6;
  24. tmp[i].a = 'a'+i;
  25. }
  26.  
  27. list->num = 5;
  28. list->v1 = calloc(5,sizeof(tmp_data));
  29.  
  30. for(i=0;i<5;i++) {
  31. list->v1[i].p1 = calloc(1,sizeof(tmp_type));
  32. }
  33. memcpy(list->v1,tmp,5*sizeof(tmp_data));
  34.  
  35. for(i=5;i<10;i++) {
  36. // memcpy((list->v1[i].p1), (tmp[i].p1), sizeof(tmp_type));
  37. // list->v1[i].a=tmp[i].a;
  38. free(tmp[i].p1);
  39. }
  40.  
  41. }
  42.  
  43. void main()
  44. {
  45. tmp_list l1;
  46. int i;
  47. get_copy(&l1);
  48.  
  49. for (i=0;i<l1.num;i++) {
  50. printf("list: %c %d %d %d\n",
  51. l1.v1[i].a,
  52. l1.v1[i].p1->x,
  53. l1.v1[i].p1->y,
  54. l1.v1[i].p1->z);
  55. }
  56.  
  57. for(i=0;i<l1.num;i++){
  58. free(l1.v1[i].p1);
  59. }
  60. free(l1.v1);
  61.  
  62. }
  63.  
  64.  
Advertisement
Add Comment
Please, Sign In to add comment