Thread: Register in C - ADT

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    2

    Register in C - ADT

    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
    Last edited by brCode; 10-19-2016 at 04:28 PM.

  2. #2
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Code:
    #include "StrCol.c"
    You probably want to include a header file instead.
    Code:
    gets(l->list[counter]);
    Don't use gets() - it's dangerous since forever, deprecated since C99 and removed from C11. One alternative is fgets().

    Start simple - if you're bent on (or required to) use a list, then start by making a simple list that holds integers. When you have that working - then change it to hold a more involved datatype.

    Hints: A list node has a member pointing at a next (and previous in case of doubly-linked list) node. You can use a one struct to represent a list node, and a separate struct to represent a list (a wrapper, sort of) that will hold pointers to head and tail nodes, to make push_back perform without always looping through the entire list.

  3. #3
    Registered User
    Join Date
    Oct 2016
    Posts
    2
    Thank you for giving attention to my question. I'm gonna follow all your clues about this program and try to make something less complex than the way I was thinking about it. After that I will improve it. If I have any doubt later I will return and post something here. Anyway if anyone has others suggestions to add to what Xupicor answered to me, make yourselves comfortable.
    Last edited by brCode; 10-20-2016 at 03:35 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. EIP register
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2008, 03:41 AM
  2. register bug
    By avgprogamerjoe in forum C++ Programming
    Replies: 8
    Last Post: 07-10-2007, 02:27 PM
  3. moving byte pointed to by contents of a register into another register. dos debug.
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-18-2003, 05:48 PM
  4. Register
    By sean345 in forum C Programming
    Replies: 7
    Last Post: 05-08-2002, 03:06 PM
  5. do we have to re-register
    By iain in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-11-2001, 08:00 AM

Tags for this Thread