Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. /* UFRB - CETEC - BCET - CET151: Processamento de Dados II
  2. * Exemplo:
  3. * Programa em C que aloca caracteres em um vetor usando malloc e realloc
  4. * Professor: Guilherme Araújo
  5. * Aluno: Samuel Rebouças
  6. * Revisão: Luiz Antônio
  7. * */
  8. #include<stdlib.h>
  9. #include<stdio.h>
  10. #include<locale.h>
  11.  
  12. int main (){
  13. int i, n, m;
  14. char *p;
  15. setlocale(LC_ALL, "portuguese");
  16. printf("informe um número inteiro \n ");
  17. scanf("%d", &n);
  18. p=(char *)malloc(n*sizeof(char));
  19. if(p==NULL){
  20. printf("error:memória insuficiente \n");
  21. }
  22. else{
  23. for(i=0; i<n; i++){
  24. setbuf(stdin,NULL);
  25. printf("\n informe o caractere de posicao %d\n", i+1);
  26. scanf("%[^\n]", &p[i]);
  27. printf("Valor armazenado com sucesso!");
  28. }
  29.  
  30. printf("\nValores armazenados:");
  31. for(i=0; i<n; i++){
  32. printf("%c", p[i]);
  33. }
  34.  
  35. m = n + 2;
  36. p=(char*)realloc(p,m*sizeof(char));
  37. printf("\ninforme os outros dois caracteres: \n");
  38. for(i=n; i<m; i++){
  39. setbuf(stdin,NULL);
  40. scanf("%[^\n]", &p[i]);
  41. }
  42.  
  43. for(i=0; i<=m; i++){
  44. printf("%c", p[i]);
  45. }
  46. }
  47. return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement