12-malloc.c 400 B

123456789101112131415161718192021222324
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main() {
  4. int *numbers = (int *)malloc(5 * sizeof(int));
  5. if (numbers != NULL) {
  6. numbers[0] = 1;
  7. numbers[1] = 2;
  8. numbers[2] = 3;
  9. numbers[3] = 4;
  10. numbers[4] = 5;
  11. int i;
  12. for (i = 0; i < 5; i++) {
  13. printf("%d ", numbers[i]);
  14. }
  15. free(numbers);
  16. } else {
  17. printf("Memory allocation failed.\n");
  18. }
  19. return 0;
  20. }