Hi everybody,
I am trying to make an adress book where I can store a persons name and his phone numbers.
I am using the Microsoft Visual C++ 2005 IDE.
There are 3 kinds of numbers :
Code:
enum number_type {MOBILE = 0, PRIVATE = 1, WORK = 2};
A contact record is represented by:
Code:
struct contact_record {
char* contact_name;
char*** numbers;
int* numbers_size;
};
typedef struct contact_record contact;
contact_name stores the name of the person, numbers is a 2D array of numbers(=strings)(1 row for each type of number), and numbers_size keeps the size of the numbers of each sort.
The tree relevant functions (where the error might occur) are:
Code:
contact init_record(char* contact_name);
void add_number(contact* c, enum number_type numbertype, char* number);
void delete_contact(contact* c);
The implementation of those functions is:
Code:
contact init_record(char* contact_name){
int i,j;
contact* c;
char*** numbers;
int* numbersize;
c = (contact*)malloc(sizeof(contact));
//contactname = malloc(20*sizeof(char));
numbers = malloc(sizeof(char**));
for(i=0; i < 3; i++) {
numbers[i] = malloc(sizeof(char*));
for(j=0; j < 1; j++)
numbers[i][j] = malloc(15*sizeof(char));
}
numbersize = calloc(3,sizeof(int));
c->contact_name = contact_name;
c->numbers = numbers;
c->numbers_size = numbersize;
return *c;
}
void add_number(contact* c, enum number_type numbertype, char* number){
switch(numbertype) {
case MOBILE:
c->numbers_size[0]++;
c->numbers[0] = realloc(c->numbers[0], c->numbers_size[0]*sizeof(char*));
c->numbers[0][c->numbers_size[0]-1] = number;
break;
// same for PRIVATE and HOME
}
}
void delete_contact(contact* c){
int i;
free(c->numbers_size);
for(i=0; i < 3; i++) {
free(c->numbers[i]);
}
free(c->numbers);
free(c->contact_name);
free(c);
}
The error occurs when reaching the line:
Is there anyone who might have an idea wath I'm doing wrong?
Thanks in advance,
Steven