fork(2) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int* copia(int *vet, int tam) {
  5. int *retorno = malloc(tam * sizeof(int));
  6. for(int i = 0; i < tam; i++)
  7. retorno[i] = vet[i];
  8. return retorno;
  9. }
  10.  
  11. int main(void) {
  12. int tam = 3;
  13. int *vet = malloc(tam * sizeof(int));
  14. vet[0] = 1;
  15. vet[1] = 2;
  16. vet[2] = 3;
  17. int *novo = copia(vet, tam);
  18. printf("0 -> %d\n", novo[0]);
  19. printf("1 -> %d\n", novo[1]);
  20. printf("2 -> %d\n", novo[2]);
  21. free(novo);
  22. return 0;
  23. }
  24.  
Success #stdin #stdout 0s 2244KB
stdin
Standard input is empty
stdout
0 -> 1
1 -> 2
2 -> 3