Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define TAILLE 4
- int* changerTailleTableau(int *tab, int ancienneTaille, int nouvelleTaille)
- {
- int i, e;
- int *tab2 = (int *)malloc(nouvelleTaille*sizeof(int));/*on alloue le nouveau tableau*/
- if (tab2==NULL)
- {
- printf("Erreur lors de l'allocation\n");
- return NULL;
- }
- for(i=0; i<ancienneTaille; i++) /*On copie le tableau*/
- {
- *(tab2+i)= *(tab+i);
- }
- if (nouvelleTaille-ancienneTaille >0)
- {
- for(e=ancienneTaille; e<nouvelleTaille; e++) /*on remplie les cases supplementaires*/
- {
- *(tab2+e)=0;
- }
- }
- return tab2;
- }
- int main()
- {
- int j, e, taille2=8;
- int *tab=(int *)malloc(TAILLE*sizeof(int));
- if (tab==NULL)
- {
- printf("Erreur lors de l'allocation\n");
- return 0;
- }
- for(j=0; j<TAILLE; j++)
- {
- *(tab+j)=j+1;
- }
- for(e=0; e<TAILLE; e++)
- {
- printf("%d\t", *(tab+e));
- }
- printf("\n");
- int *tab2 = NULL;
- tab2 = changerTailleTableau(tab, TAILLE, taille2);
- for(e=0; e<taille2; e++)
- {
- printf("%d\t", *(tab2+e));
- }
- printf("\n");
- free(tab);
- free(tab2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement