To implement the address Book dynamically, you need to use these functions such as malloc and realloc, first let's introduce these functions.
(1) void *malloc (unsigned int size);
Size is the number of bytes that need to be allocated.
(2) void *calloc (unsigned int num_elements,unsigned int elements_size);
Num_elements is the number of elements allocated, Elements_size is the number of bytes per element.
(3) void *realloc (void *p,unsigned int new_size);
New_size is the modified number of bytes, and P is the original memory header address.
Since these functions are space in the heap, they need to be freed after use, so the free function is required.
(4) Void free (void *p);
P is the first address of the space that needs to be freed.
The first 3 functions require free, and if not, a memory leak can occur. The free release needs to be set to NULL when it is finished. We must have a principle before using the pointer, that is, before using the first to determine whether it is empty, and then need to be null.
We're here to implement the dynamic growth Address book.
The code is as follows:
#define _CRT_SECURE_NO_WARNINGS 1#ifndef __CONTACT_H__#define __CONTACT_H__#include <stdio.h> #include <string.h>enum op{exit,add,del,modify,search,display,sort,clear};# Define name_max 20#define sex_max 5#define tele_max 12#define addr_max 25#define max 1000#define max_init 2#define max_rise 2typedef struct PerInfo{char name[NAME_MAX];int age;char sex[SEX_MAX];char tele[TELE_MAX];char Addr[addr_max];} Peoinfo;typedef struct contact{peoinfo data[max];int size;int i;} Contact,*pcon;void init_contact (Pcon pcon); Void _add_contact (Pcon pcon); void _display _contact (Pcon pcon); Void _del_contact (Pcon pcon); Void _search_contact (Pcon pcon); void _modify_contact (Pcon pcon); Void _sort_contact (Pcon pcon); Void _clear_contact (Pcon pcon); #endif //__Contact_h__
function Implementation section:
#define _CRT_SECURE_NO_WARNINGS 1#include "Contact.h"////git//svn//void init_contact (Pcon pcon) {//memset (pcon->data, 0, max*sizeof (peoinfo));//pcon->size = 0;pcon-> Data= (perinfo *) malloc (max_init*sizeof (Perinfo)); if (Pcon->data == null) { printf ("Out of menory"); exit (EXIT_ FAILURE); } pcon->size=0; pcon->capacity=max_init; }void _add_contact (Pcon pcon) {/*if ( Pcon->size >= max) {printf ("The phone is full \ n"); return;} */if (pcon->size >= pcon->i) { perinfo *tmp= (perinfo *)ReAlloc (Pcon->data, (pcon->i+max_rise) *sizeof (perinfo)); //with realloc capacity when the actual number of contacts is equal to the amount of initialization if (tmp == null) { printf ("out of menory\n"); exit (EXIT_FAILURE); } else { pcon->data=tmp; pcon->capacity+=MAX_RISE; } printf ("Please enter name:>"), scanf ("%s", Pcon->data[pcon->size].name);p rintf ("Please enter Age:>"); scanf ("%d",& ( pcon->data[pcon->size].age));p rintf ("Please enter Gender:>"), scanf ("%s", Pcon->data[pcon->size].sex);p rintf (" Please enter Phone:> "), scanf ("%s ", Pcon->data[pcon->size].tele);p rintf (" Enter Address:> "), scanf ("%s ", pcon->data[pcon- >SIZE].ADDR);p con->size++;p rintf ("Add success \ n");} Void _display_contact (Pcon pcon) {int i = 0;printf ("%9s\t%3s\t%4s\t%11s\t%10s\n", "name "," Age "," sex "," Tele "," addr "), and for (i = 0;i<pcon->size;i++) {printf ("%9s\t%3d\t%4s\t%11s\t%10s\n ", PCON->DATA[I].NAME,PCON->DATA[I].AGE,PCON->DATA[I].SEX,PCON->DATA[I].TELE,PCON->DATA[I].ADDR);}} Static int find_entry (pcon pcon, char *name) {int i = 0;for (i = 0;i<pcon->size; i++) {if (strcmp (pcon->data[i].name,name) == 0) {return i;}} Return -1;} Void _del_contact (Pcon pcon) {int pos =&Nbsp;0;int index = 0;char name[name_max];if (pcon->size == 0) {printf ("Phone book empty \ n") ; return;} printf ("Please enter the name of the person to be deleted:>"); scanf ("%s", name);p os = find_entry (pcon, name); if (pos == -1) {printf ("Cannot find the person to delete \"); return;} for (index = pos; index < pcon->size; index++) {pcon->data[index] =&NBSP;PCON->DATA[INDEX+1];} pcon->size--;p rintf ("delete succeeded \ n");} Void _search_contact (Pcon pcon) {char name[name_max] = {0};int pos = 0;printf ("Please enter the name of the person to find:>"), scanf ("%s", name);p os = find_entry (pcon, name); if (pos == -1) {printf ("The specified contact does not exist \ n"); return;} else{printf ("%9s\t%3s\t%4s\t%11s\t%10s\n", "name", "Age", "sex", "Tele", "addr");p rintf ("%9s\t%3d\t%4s\t%11s\t%10s\ N ", Pcon->data[pos].name,pcon->data[pos].age,pcon->data[pos].sex,pcon->data[pos].tele,pcon->data [pos].addr);}} Void _modify_contact (Pcon pcon) {Char name[name_max] = {0};int pos = 0;printf ("Please enter the name of the person to be modified:>"), scanf ("%s", name);p Os = find_entry ( Pcon, name); if (pos == -1) {printf ("The specified contact does not exist \ n"); return;} else{printf ("Please enter name:>"), scanf ("%s", Pcon->data[pos].name);p rintf ("Please enter Age:>"), scanf ("%d",& (pcon-> data[pos].age));p rintf ("Please enter Gender:>"), scanf ("%s", Pcon->data[pos].sex);p rintf ("Please enter phone:>"); scanf ("%s", pcon- >data[pos].tele);p rintf ("Please enter Address:>") scanf ("%s", pcon->data[pos].addr);}} Volatilevoid _sort_contact (Pcon pcon) {int i = 0;int j = 0;for (i = 0;i<pcon->size-1; i++)//control sort trip number {for (j = 0; j<pcon->size-1-i; J + +) {if (strcmp (pcon->data[j].name,pcon->data[j+1].name) > 0) {peoinfo tmp = { 0};TMP&NBSP;=&NBSP;PCON->DATA[J];p con->data[j] = pcon->data[j+1];p con->data[j+1] = tmp;}}}} Void _clear_contact (Pcon pcon) {pcon->size = 0;}
This article is from the "10917138" blog, please be sure to keep this source http://10927138.blog.51cto.com/10917138/1773901
Implementation of Address Book (ii) Dynamic implementation