Advertisement
Eastkap

x jul

Oct 30th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define TAILLE 4
  4.  
  5.  
  6.  
  7. int* changerTailleTableau(int *tab, int ancienneTaille, int nouvelleTaille)
  8. {
  9.     int i, e;
  10.     int *tab2 = (int *)malloc(nouvelleTaille*sizeof(int));/*on alloue le nouveau tableau*/
  11.     if (tab2==NULL)
  12.     {
  13.         printf("Erreur lors de l'allocation\n");
  14.         return NULL;
  15.     }
  16.     for(i=0; i<ancienneTaille; i++) /*On copie le tableau*/
  17.     {
  18.         *(tab2+i)= *(tab+i);
  19.     }
  20.     if (nouvelleTaille-ancienneTaille >0)
  21.     {
  22.         for(e=ancienneTaille; e<nouvelleTaille; e++) /*on remplie les cases supplementaires*/
  23.         {
  24.             *(tab2+e)=0;
  25.         }
  26.     }
  27.     return tab2;
  28. }
  29.  
  30. int main()
  31. {
  32.     int j, e, taille2=8;
  33.     int *tab=(int *)malloc(TAILLE*sizeof(int));
  34.     if (tab==NULL)
  35.     {
  36.         printf("Erreur lors de l'allocation\n");
  37.         return 0;
  38.     }
  39.     for(j=0; j<TAILLE; j++)
  40.     {
  41.         *(tab+j)=j+1;
  42.     }
  43.    
  44.     for(e=0; e<TAILLE; e++)
  45.     {
  46.         printf("%d\t", *(tab+e));
  47.     }
  48.     printf("\n");
  49.    
  50.     int *tab2 = NULL;
  51.     tab2 = changerTailleTableau(tab, TAILLE, taille2);
  52.    
  53.     for(e=0; e<taille2; e++)
  54.     {
  55.         printf("%d\t", *(tab2+e));
  56.     }
  57.     printf("\n");
  58.    
  59.     free(tab);
  60.     free(tab2);
  61.    
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement