1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define TAILLE_MAX 20
typedef struct abonne_ Abonne;
/*L'entreprise Prog3com désire créer son propre réseau téléphonique. Celui-ci est composé d'abonnés,
identifiés par leur nom, prénom et numéro d'appel (à 10 chiffres).*/
struct abonne_{
char* nom;
char* prenom;
int num;
};
typedef Abonne** reseau;
/*déclarations de fonctions*/
Abonne* creeAbonne(void);
void detruit(Abonne** a);
/*creeAbonne qui crée un nouvel abonné. Les différents champs seront ici introduits via le
clavier.*/
Abonne* creeAbonne(void){
Abonne* a = malloc(sizeof(Abonne));
if(a != NULL){
int num;
char* nom = calloc(TAILLE_MAX+1,sizeof(char));
char* prenom = calloc(TAILLE_MAX+1,sizeof(char));
do {
printf("Rentrez son nom \n");
scanf("%s",&(*nom));
}while(strlen(nom) > TAILLE_MAX+1);
do {
printf("Rentrez son prénom \n");
scanf("%s",&(*prenom));
}while(strlen(prenom) > TAILLE_MAX+1);
printf("Rentrez son numero \n");
scanf("%d",&num);
strncpy(a->nom,nom,strlen(nom)+1);
strncpy(a->prenom,prenom,strlen(prenom)+1);
a->num = num;
free(nom);
free(prenom);
}
}
/*désallocations*/
void detruit(Abonne** a){
if((a == NULL) || (*a == NULL)){
fprintf(stderr, "Null pointer : Problème de destruction");
}
else {
free(&(*a)->nom);
free(&(*a)->prenom);
free(&(*a)->num);
free(*a);
*a=NULL;
}
}
int main(void){
Abonne* a = creeAbonne();
detruit(&a);
} |
Partager