fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. // core data structure
  5. struct Point
  6. {
  7. float x;
  8. float y;
  9. float z;
  10. };
  11.  
  12. // function that allocates space for our array and fills it with dummy values
  13. void fill_xyz_list(struct Point **xyz_list_ptr, int list_size)
  14. {
  15. // allocate memory for 100 Point structs
  16. *xyz_list_ptr = realloc(*xyz_list_ptr, sizeof(struct Point)*list_size);
  17.  
  18. // set values for each member
  19. int i;
  20. for (i=0; i<list_size; i++)
  21. {
  22. (*xyz_list_ptr)[i].x = i + 0.1;
  23. (*xyz_list_ptr)[i].y = i + 0.2;
  24. (*xyz_list_ptr)[i].z = i + 0.3;
  25. }
  26. }
  27.  
  28. int main()
  29. {
  30. struct Point *xyz_list = NULL; // our array of (x, y, z)
  31. int list_size; // our array size
  32. int i;
  33.  
  34. // set list size
  35. list_size = 10;
  36.  
  37. // fill xyz_list array with dummy values
  38. fill_xyz_list(&xyz_list, list_size);
  39.  
  40. // print all members
  41. for (i=0; i<list_size; i++)
  42. {
  43. printf("xyz_list[%d]: x=%.2f, y=%.2f, z=%.2f\n", i, xyz_list[i].x, xyz_list[i].y, xyz_list[i].z);
  44. }
  45.  
  46. return 0;
  47. }
  48.  
Success #stdin #stdout 0s 2300KB
stdin
Standard input is empty
stdout
xyz_list[0]: x=0.10, y=0.20, z=0.30
xyz_list[1]: x=1.10, y=1.20, z=1.30
xyz_list[2]: x=2.10, y=2.20, z=2.30
xyz_list[3]: x=3.10, y=3.20, z=3.30
xyz_list[4]: x=4.10, y=4.20, z=4.30
xyz_list[5]: x=5.10, y=5.20, z=5.30
xyz_list[6]: x=6.10, y=6.20, z=6.30
xyz_list[7]: x=7.10, y=7.20, z=7.30
xyz_list[8]: x=8.10, y=8.20, z=8.30
xyz_list[9]: x=9.10, y=9.20, z=9.30