Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct _contact {
- const char *prenom;
- const char *nom;
- const char *adresse;
- } contact;
- typedef struct _carnet {
- contact *contacts;
- size_t capacity;
- int count;
- } carnet;
- void extend_carnet (carnet *crt, unsigned int s);
- carnet new_carnet (void) {
- carnet crt;
- crt.contacts = NULL;
- crt.capacity = 0;
- crt.count = 0;
- extend_carnet (&crt, 3);
- return crt;
- }
- void delete_carnet (carnet *crt) {
- if (crt->contacts != NULL) {
- free (crt->contacts);
- crt->contacts = NULL;
- }
- crt->capacity = 0;
- crt->count = 0;
- }
- void extend_carnet (carnet *crt, unsigned int s) {
- crt->capacity += s;
- crt->contacts = realloc (crt->contacts,
- crt->capacity*sizeof (*crt->contacts));
- }
- void show_carnet (const carnet *crt) {
- int i;
- for (i=0; i < crt->count; i++) {
- printf ("Contact nĀ°%i : %s\n", i+1,
- crt->contacts[i].prenom);
- }
- }
- void add_contact (carnet *crt, contact cnt) {
- if (crt->capacity == crt->count) {
- extend_carnet (crt, 3);
- }
- crt->contacts[crt->count] = cnt;
- crt->count++;
- }
- int main (int argc, char **argv) {
- carnet crt = new_carnet ();
- contact steven;
- steven.prenom = "StevenDeo";
- add_contact (&crt, steven);
- add_contact (&crt, steven);
- add_contact (&crt, steven);
- add_contact (&crt, steven);
- add_contact (&crt, steven);
- add_contact (&crt, steven);
- add_contact (&crt, steven);
- add_contact (&crt, steven);
- show_carnet (&crt);
- delete_carnet (&crt);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement