fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(void) {
  5. int capacity = 4; // for example
  6.  
  7. void **array = malloc(capacity*sizeof(void*));
  8. // assign values to array elements
  9. for(int i = 0; i < capacity; i++) {
  10. array[i] = malloc(sizeof(int)); // not sure if it necessary
  11. *(int*)array[i] = i*i;
  12. printf("index: %d, element: %d\n", i, *(int*)array[i]); // for demonstration
  13. }
  14. printf("\n");
  15. /*
  16.   * after that I try to print all the elements of the array sequentially
  17.   */
  18. for(int i = 0; i < capacity; i++) {
  19. printf("index: %d, element: %d\n", i, *(int*)array[i]);
  20. }
  21.  
  22. // I know that I do not free my memory, but that’s not the point
  23. return 0;
  24. }
  25.  
Success #stdin #stdout 0s 4180KB
stdin
Standard input is empty
stdout
index: 0, element: 0
index: 1, element: 1
index: 2, element: 4
index: 3, element: 9

index: 0, element: 0
index: 1, element: 1
index: 2, element: 4
index: 3, element: 9