Passing a dynamic array to a function

Joined
Jun 5, 2010
Messages
1
Reaction score
0
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:

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?
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top