Good evening to all
I wish make a data register in Abstract Data Type(ADT) , where there are a main.c, a StrCol.c and a StrCol.h(header) files. Among the data I wanna register are : name, a list of phone numbers and a list of e-mails type StrCol. In this way I intend to insert in the list and to list data referring to a contact.
I've created a variable named as BLOCK to define size of each block on memory and a MAX variable to inform the maximum quantity of blocks it should have. Following this thought, when you try to insert a data in list , it is first verified if a contact exists and if the blocks are filled. If there's no contact it is allocated on memory a pointer array(type char) where each one of them will point to a memory block, according to data size user is entering. If there's a contact and if blocks were already filled and not exceed the maximum block numbers, so it is done a memory reallocation, where it is available a new block without loosing old data. I've created in the StrCol.h file , a StrcCol struct where inside it I have named list pointer of pointer variable. But some information are mismatched because I had already defined BLOCK and COUNTER fields inside StrCol.h file , but I don't know how to call that in the StrCol.c file.
I am very confused and I'm finding it very difficult on how to create a contact list inside main.c function and how to list data like name, phone list and email list from each contact . Any help will be very appreciated . Thanks in advance.
Code:
==================Inside main.c=====================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "StrCol.c"
struct contact
{
char name[50];
StrCol phones;
StrCol emails;
};
int main(void)
{
struct contact c1;
}
===================Inside StrCol.c===================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "StrCol.h"
#define BLOCK 4
#define MAX 2
int counter = 0;
int block_nums = 0;
//Create a generic list adapted to insert phone and emails.
void insert(StrCol *l , char *s)
{
if((counter != 0) && (counter % BLOCK == 0 ))
{
if(block_nums >= MAX )
{
printf("Full Schedule!\n");
return;
}
l->list = (char**) realloc(l->list ,(blocks_nums + 1) * BLOCK *
sizeof(char*));
block_nums++;
}
else
{
l->list = (char**) malloc (BLOCK * sizeof(char*));
block_nums++;
l->list[counter] = (char*) malloc(strlen(s) * sizeof(char));
gets(l->list[counter]);
counter++;
}
}
void list(StrCol *l)
{
}
====================Inside StrCol.h==================
#ifndef STRCOL_H_INCLUDED
#define STRCOL_H_INCLUDED
typedef struct _col
{
int BLOCK ;
int COUNTER ;
char **list;
}StrCol;
void insert(StrCol *l , char *s);
void list(StrCol *l);
#endif // STRCOL_H_INCLUDED