-
Notifications
You must be signed in to change notification settings - Fork 2
/
contactList.c
87 lines (58 loc) · 1.4 KB
/
contactList.c
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* This is an example of structs, typedef, and a simple list using dynamic
* memory allocation
*
* @author J. Alvarez
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 50
typedef char String[MAX_SIZE];
typedef struct {
String name;
int age;
String email;
} Contact;
void read(char * buf) {
scanf("%49s",buf);
}
void showList(Contact * contacts, int n) {
printf("This is your contact list:\n");
printf("Name\t\tAge\t\tEmail\n");
int i;
for(i=0; i<n; i++)
printf("%s\t\t%d\t\t%s\n",contacts[i].name, contacts[i].age,
contacts[i].email);
}
int main() {
int stop = 0;
char buffer[MAX_SIZE];
Contact * list = NULL;
int listSize = 0;
do {
printf("Please enter the new contact's name, or 0 to exit:\n");
read(buffer);
if(strcmp("0",buffer) != 0) {
if(!list)
list = malloc(sizeof(Contact));
else
list = realloc(list,sizeof(Contact) * (listSize + 1));
strcpy(list[listSize].name, buffer);
printf("Please enter the contact's age:\n");
read(buffer);
list[listSize].age = atoi(buffer);
printf("Please enter the contact's email:\n");
read(buffer);
strcpy(list[listSize].email, buffer);
listSize++;
printf("\n------------------Saved--------------------\n\n");
}
else
stop = 1;
} while(!stop);
showList(list, listSize);
printf("Program finished. Cleaning memory\n\n");
free(list);
return 0;
}