that_one_nigga_you_k

teste realloc

Jul 20th, 2023
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.69 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main() {
  5.  
  6.     int* num = NULL;
  7.     int tam = 0;
  8.     int fim = 1;
  9.     int ent;
  10.  
  11.     while (fim != 0) {
  12.         printf("\ndigite um numero: ");
  13.         scanf("%d", &ent);
  14.  
  15.         if (ent == 0) {
  16.             fim = 0;
  17.         } else {
  18.             num = realloc(num, (tam + 1) * sizeof(int));
  19.             if (num == NULL) {
  20.                 printf("Memory allocation failed.\n");
  21.                 return 1;
  22.             }
  23.             num[tam] = ent;
  24.             tam++;
  25.         }
  26.     }
  27.  
  28.     printf("\no que vc digitou: ");
  29.     for (int i = 0; i < tam; i++) {
  30.         printf("\n%d", num[i]);
  31.     }
  32.  
  33.     free(num);
  34.     return 0;
  35. }
  36.  
Add Comment
Please, Sign In to add comment