Thread: Array of Structures

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Array of Structures

    I am in need of help creating a program. I have been told I need to create a program that reads from a database table into a structure and then displays the data on the screen. I am in over my head so I figured while I am trying to get this started I would ask for some direction. I used the code below to read contents I created but now I need to get the data from a database table. Thanks in advance for any help.

    Code:
    #include <stdio.h>
    
    struct cc_lkup_ {
        short   cc;
        char    cc_suf[2];
        char    cc_sn[11];
        char    cc_desc[41];
        short   prd_rpt_id;
        short   rpt_cc;
        char    sfdc_grp[41];
        char    cc_grp[41];
        char    proc_grp[3];
        char    proc_grp_desc[16];
        short   cc_typ;
        char    resp_company[4];
        char    rpt_tbl[33];
      } cc_lkup_;
    
    
    main ()
    {
    
      memset (&cc_lkup_, 0, sizeof(cc_lkup_));
    
      int count=0;
    
      struct cc_lkup_ record[]=
      {
        {11,"ab","cd","ef",21,31,"gh","ij","kl","mn",100,"Car","qr"},
        {11,"ab","cd","ef",21,31,"gh","ij","kl","mn",101,"Bob","qr"},
        {11,"ab","cd","ef",21,31,"gh","ij","kl","mn",102,"Ric","qr"},
        {11,"ab","cd","ef",21,31,"gh","ij","kl","mn",999,"Fail","qr"},
      };
    
    /* PRINT TO SCREEN */
    
      while( record[count].cc_typ != 999)
      {
        printf("resp_company field holds this %s \tcc_typ is %d \n", record[count].resp_company, record[count].cc_typ);
        count++;
      }
    }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
      memset (&cc_lkup_, 0, sizeof(cc_lkup_));
    I am not clear as to what you are trying to accomplish here. I guess perhaps you are wanting to use cc_lkup_ as the variable to read from the table. Firstly, I suggest not having the structure and variable names identical. Though entirely legal in C, its not easy for anyone reading your code to follow (nor is it legal in C++).

    Have you tried using the memcpy() function? I will let you google it this time since I would feel dirty doing your homework for you.

    On a side note, I see in your code that dumps the records to the screen that you go all the way to 999 whilst only having 4 entries. This is called buffer overflow and will get you flogged in tarred in some programming circles.
    Last edited by master5001; 04-11-2008 at 02:30 PM.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    12

    Hmmm

    I'm pretty new to C programming (about two weeks), but I think you're defining your structure wrong. Perhaps it should be (I'll use typedef because I get annoyed typing struct):

    Code:
    typedef struct {
        short   cc;
        char    cc_suf[2];
        char    cc_sn[11];
        char    cc_desc[41];
        short   prd_rpt_id;
        short   rpt_cc;
        char    sfdc_grp[41];
        char    cc_grp[41];
        char    proc_grp[3];
        char    proc_grp_desc[16];
        short   cc_typ;
        char    resp_company[4];
        char    rpt_tbl[33];
     } cc_lkup_ ;
    I'd not worry about the memset; I think you're trying to zero the structure that you haven't actually allocated yet. Instead, just skip it and define your contents as you have there (remove the "struct" if you use the typedef as I have above):

    Code:
    cc_lkup_ record[4] =  {
        {11,"ab","cd","ef",21,31,"gh","ij","kl","mn",100,"Car","qr"},
        {11,"ab","cd","ef",21,31,"gh","ij","kl","mn",101,"Bob","qr"},
        {11,"ab","cd","ef",21,31,"gh","ij","kl","mn",102,"Ric","qr"},
        {11,"ab","cd","ef",21,31,"gh","ij","kl","mn",999,"Fail","qr"},
     };
    Then fix your loop:

    Code:
    int i;
    for( i = 0; i < 4; i++ ) {
        printf("resp_company field holds this &#37;s \tcc_typ is %d \n", record[i].resp_company, record[i].cc_typ);
    }
    Maybe? Like I wrote, I'm a bit of a newb.
    Last edited by maestro371; 04-11-2008 at 05:17 PM. Reason: Fixed my curly brace

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  2. filling an array of structures?
    By voodoo3182 in forum C Programming
    Replies: 9
    Last Post: 08-06-2005, 05:29 PM
  3. Array of Structures: Sorting
    By Drainy in forum C Programming
    Replies: 3
    Last Post: 04-13-2005, 09:55 AM
  4. Filling an Array of Structures
    By Zildjian in forum C Programming
    Replies: 5
    Last Post: 11-12-2003, 05:54 PM
  5. Need serious help on array of structures
    By cwd in forum C Programming
    Replies: 2
    Last Post: 11-11-2001, 03:39 PM