fixedynarr.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include <stdio.h>
  2. #include <stddef.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. struct IntArray{
  6. int *data;
  7. size_t length;
  8. };
  9. void readintarray(struct IntArray *input) {
  10. printf("inputlength:%zu\n", input->length);
  11. for (int i = 0; i < input->length; i++) {
  12. printf("digit:%d = %d\n", i, input->data[i]);
  13. }
  14. }
  15. int main() {
  16. srand(time(NULL));
  17. printf("how many numbers?\n");
  18. int length;
  19. scanf("%d", &length);
  20. struct IntArray *myintarray = malloc(sizeof(struct IntArray)); /* allocate memory for struct */
  21. int *myarray = malloc(sizeof(int) * length); /* allocate memory for array that the struct points to */
  22. myintarray->data = myarray; /* make sure the pointer in the struct points to the array */
  23. myintarray->length = length; //forgot to actually assign a value to the struct
  24. for (int i = 0; i < length; i++) {
  25. myintarray->data[i] = rand() % 10;
  26. printf("number:%d = %d\n",i,myintarray->data[i]);
  27. }
  28. readintarray(myintarray);
  29. free(myintarray->data);
  30. free(myintarray);
  31. return 0;
  32. }