-
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++;
}
}
-
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.
-
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 %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.