fork download
  1. #include <stdio.h>
  2.  
  3. struct dblarr {
  4. double *data;
  5. size_t len;
  6. struct dblarr *next;
  7. };
  8.  
  9. double *fetch(const struct dblarr *arr, size_t index) {
  10. if (arr == NULL) return NULL;
  11. if (index < arr->len) return arr->data + index;
  12. return fetch(arr->next, index - arr->len);
  13. }
  14.  
  15. int main(void) {
  16. double a1[2] = {1, 2};
  17. double a2[3] = {1, 2, 3};
  18. double a3[1] = {1};
  19. struct dblarr x1, x2, x3;
  20.  
  21. x1.data = a1; x1.len = sizeof a1 / sizeof *a1; x1.next = &x2;
  22. x2.data = a2; x2.len = sizeof a2 / sizeof *a2; x2.next = &x3;
  23. x3.data = a3; x3.len = sizeof a3 / sizeof *a3; x3.next = NULL;
  24.  
  25. printf("before %f\n", *fetch(&x1, 5));
  26. *fetch(&x1, 5) = 0.42;
  27. printf(" after %f\n", *fetch(&x1, 5));
  28.  
  29. return 0;
  30. }
Success #stdin #stdout 0.01s 1720KB
stdin
Standard input is empty
stdout
before 1.000000
 after 0.420000