Can Array Work within Structure?

K

kimimaro

hi I wonder if array can be work along with structure?

Below are the declaration of my structure



struct employee{
char ID[15];
char Name[20];
char Department[5][30];
int selection;
char Post[3][30];
};
struct employee record[200];


I was wondering could if there is anywhere you can :


struct employee{
char ID[15];
char Name[20];
char Department[5][30]={"Accounting","Administration","Management","Human
resource","Others"};
int selection;
char Post[3][30];
};
struct employee record[200];


because the problem is when I wanted to store this data , below is the
declaration of arrays within the add_record function

char Department[5][30]={"Accounting","Administration","Management","Human
Resource","Others"};

Here i use to capture the inputs and department as selection :

printf("\n\n 0) %s\n 1) %s\n 2) %s\n 3) %s\n\n [Enter Department for this
new employee]: ", Department[0], Department[1], Department[2],
Department[3]);
fflush(stdin);
scanf("%d", &selection);
if((selection != 0) && (selection != 1) && (selection !=2) && (selection
!=3)) {gotoxy(1, 24); printf(" >>invalid Department<< Retry!\a");}
} while(selection < 0 || selection > 3);


clrscr();
printf("\n Enter The new ID for this employee: ");
fflush(stdin);
gets(ID);
printf("\n Employee Name : ");
fflush(stdin);
gets(Name);

You can only display the contents such as Accounting, Administration when
you printf as Department[selection]

But how should I do if i want it "for example Management" to store in the
structure so that next time when I printf Department for that particular
record in other function it would appear as Management and not blank
because mine when I printf it on other function it is null? The Name, Post
and ID works out fine but the Department cannot stored in the structure as
selection.
 
S

Stuart Gerchick

I am not sure why you want to store all 5 departments in each
employee. Structs can definetly contain arrays. But this data is
static. There is no need for this in each employee. Just the choice
of department is enough
 
B

Barry Schwarz

hi I wonder if array can be work along with structure?

Yes, a member of a structure may be an array.
Below are the declaration of my structure



struct employee{
char ID[15];
char Name[20];
char Department[5][30];
int selection;
char Post[3][30];
};
struct employee record[200];

This all appears syntactically correct.
I was wondering could if there is anywhere you can :


struct employee{
char ID[15];
char Name[20];
char Department[5][30]={"Accounting","Administration","Management","Human
resource","Others"};
int selection;
char Post[3][30];
};

You cannot specify the initialization in the *declaration* of the
struct type. You can only specify initialization in the *definition*
of an object with that type.
struct employee record[200];

To be consistent with the intent of the above declaration, you would
use something like

struct employee record[200] = {
{ /* the values for record[0] */ },
{ /* the values for record[1] */ },
................. /* sample values for record */
{ "123456789abcde", /* init value for ID */
"abcdefghijklmnopqrs", /* init value for Name */
{"Accounting",
"Administration",
"Management",
"Human resource",
"Others"}, /* init values for Department */
12, /* init value for selection */
{"post 1",
"post 2",
"post 3"}}, /* init values for Post */
.......... /* as many more sets of init values as you like */
}
because the problem is when I wanted to store this data , below is the
declaration of arrays within the add_record function

char Department[5][30]={"Accounting","Administration","Management","Human
Resource","Others"};

Here i use to capture the inputs and department as selection :

printf("\n\n 0) %s\n 1) %s\n 2) %s\n 3) %s\n\n [Enter Department for this
new employee]: ", Department[0], Department[1], Department[2],
Department[3]);
fflush(stdin);

fflush is not defined for input streams.
scanf("%d", &selection);
if((selection != 0) && (selection != 1) && (selection !=2) && (selection
!=3)) {gotoxy(1, 24); printf(" >>invalid Department<< Retry!\a");}

Make life easy on yourself. Simply check for outside the desired
range.
} while(selection < 0 || selection > 3);

Just like you did here.
clrscr();
printf("\n Enter The new ID for this employee: ");
fflush(stdin);
gets(ID);

gets used this way cannot prevent overrun if the user enters too many
characters.
printf("\n Employee Name : ");
fflush(stdin);
gets(Name);

You can only display the contents such as Accounting, Administration when
you printf as Department[selection]

But how should I do if i want it "for example Management" to store in the
structure so that next time when I printf Department for that particular
record in other function it would appear as Management and not blank
because mine when I printf it on other function it is null? The Name, Post
and ID works out fine but the Department cannot stored in the structure as
selection.

Your structure already stores all possible departments for each
element of the array so you need to rethink the question.



<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top