Hello I have a structure called Company.
struct Company
{
char *employee;
char *employee_address;
};
I want to build an array of this structure but the number of employees
will change thorughout the course the programs use so it will need to
be dynamic.
I know I need to first allocate the structure and then allocate the
array of structures but I am confussed on how to do this.
In my program I am doing the following
Also for right now I am using
#Define count = 100; until I get the count function setup properly to
get the exact count in the future
void u_setup_company_array(int employee_number)
{
int q;
struct Company CompArray;
/*Allocate the array structure */
CompArray= (struct Company ) malloc(count* sizeof(struct Company
*));
/*if bad allocation then return */
if(!CompArray)
{
return;
}
/*allocate the array and set the variables to Null */
for(q = 0; q<= count ; h++)
{
CompArray[q] = (struct Company ) malloc(sizeof(CompArray));
CompArray[q]->employee= NULL;
CompArray[q]->employee_address= NULL;
}
/*Once the array is allocated then I pass it and the employee
number to the other function */
status = build_company_array(employe_number, CompArray);
<----Should this be &ComArray instead?
In another module I would have the function build_company_array
long build_company_array(int employee_number, structure CompArray)
{
/* Read the database and find a match on the employee number */
int counter = 0;
while (database_emp#)
{
if (database_emp# == employee_number)
{
CompArray[count]employee = getName(employee_number);
CompArray[count]employee_address =
getAddress(employee_number);
counter++;
}
}
return 0;
}
I guess my questions pertain to proper allocation of the structure and
the array. It's a simple straight forward structure so I don't think I
need to make it a pointer to a structure do I?
Anyway
1) Am I defining my structure properly in my initial function? Should
it be struct Company CompArray; or struct Company *CompArray;
2) Am I allocating the structure properly?
3) Am I allocating the array properly?
4) Am I passing the array properly so that when it returns it will be
populated with the right values that it built in the called function?
What I want to do is setup the array in the first u_setup_company_array
function pass the allocated array to the build_company_array function
in the other module. Once in the other module I populate it. When it
comes returns to the u_setup_company_array function then it will be
populated and ready for use in the next step of the
u_setup_company_array function
Once done with everything in the array then I would get rid of it which
i understand what to do there.
can you guys help?
Thanks
Sam
BTW I am using using c modules compiled in Visual Studios.Net