Hello!
I'm having some problems with dynamic arrays passed by reference.
I have an add_employee() function which receives a pointer to a dynamic array of Employee structures.
The purpose of this function is to add employees to an array, everytime this function is callled and reallocate the size of the array to accomodate new employees.
The problem is I'm given an "Unhandled Exception" each time the function is called for the second time(to add a second employee to the array).The first time it works alright and the array is filled with the information passed on the call to gets();
Here's the Employee function:
If I use this code directly in main it works alright,but in this function it doesn't.
What could I be doing wrong?
I'm having some problems with dynamic arrays passed by reference.
I have an add_employee() function which receives a pointer to a dynamic array of Employee structures.
The purpose of this function is to add employees to an array, everytime this function is callled and reallocate the size of the array to accomodate new employees.
The problem is I'm given an "Unhandled Exception" each time the function is called for the second time(to add a second employee to the array).The first time it works alright and the array is filled with the information passed on the call to gets();
Here's the Employee function:
Code:
int add_employee(Employee **employee,int n_employees)
{
n_employees++;
*employee=(Employee*) realloc(*employee,n_employees* sizeof(Employee));
if(*employee== NULL)
{
fprintf(stderr, "Error alocating memory!");
exit(1);
}
printf("Introduce employee name:\n");
fflush(stdin);
gets(employee[n_employees-1]->name);
employee[n_employees-1]->id=n_employees;
return n_employees;
}
If I use this code directly in main it works alright,but in this function it doesn't.
What could I be doing wrong?