Passing a dynamic array to a function

This is a discussion on Passing a dynamic array to a function within the C Programming forums, part of the General Programming Boards category; Hello! I'm having some problems with dynamic arrays passed by reference. I have an add_employee() function which receives a pointer ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    37

    Post Passing a dynamic array to a function

    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?

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Is it C ?? unhandled exception?
    Post how you call function,pass parameter,etc.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    Yes,it's C.
    Here it is the main function:

    Code:
    void main()
    {
            int n_employees=0;
    	Employee *emp=NULL;
    
    	.........
    		
    	emp=(Employee *)malloc(sizeof(Employee));
    
    		if(emp==NULL)
    		{
    			fprintf(stderr,"Error alocating memory!"); 
    			exit (1); 
    		}
    			
    
       
        do{
           ...
      
                case 2: 		
    				do
    				{
    					...
    					switch(opsub)
    					{
    						case 1:
    					n_employees=add_employee(&emp,n_employees);
                                                    break;
                                             ...
                                            }
                                    } while(opsub!=5);
    							
               }while(op!=5);

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    is name field in Employee array ?? How is Employee defined ?
    Use fgets() rather than gets().
    fflush(stdin)
    c-faq

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    The Employee structure in Employee.h:

    Code:
    typedef struct employee
    {
        char name[100];
        int id;
    
    }Employee;
    
    
    int add_employee(Employee **employee,int n_employees);

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Can't you simplify this? say like :
    Code:
    int add_employee(Employee **employee,int n_employees)
    
    int main(void)
    {
       int i;
       int n = 0;
       Employee *emp  = NULL;
       for(i = 0; i < 5; i++) {
          n = add_employee(&emp,n);
       }
    
      return 0;
    }

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    Unfortunately, no as the number of employees is undefined and that for cycle would limit the call to the function add_employee to 5 times.

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You are not getting what i meant. It's to isolate your add_employee function. I guess the problem is somewhere else.

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    Quote Originally Posted by Bayint Naung View Post
    You are not getting what i meant. It's to isolate your add_employee function. I guess the problem is somewhere else.
    I've tried what you said,isolating it,but the "Unhandled Exception...Access Violaton" persists and it only appears when i try to add a second employee...The line of code where it breaks for the second time is gets(employee[n_employees-1]->name); and the value i see on Visual studio is some hexadecimal with <Bad Ptr> writen
    The strange thing is that the 1st time it works just fine!

  10. #10
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    	gets( (*employee)[n_employees-1].name);
    	(*employee)[n_employees-1].id=n_employees
    ;

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    Quote Originally Posted by Bayint Naung View Post
    Code:
    	gets( (*employee)[n_employees-1].name);
    	(*employee)[n_employees-1].id=n_employees
    ;
    Thank you!I was struggling with that for a long time!
    Just one last question: if I wanted to print the contents of each array,how would it look like?Currently I have:


    Code:
    for(i=0;i<n_employees;i++)
    	{
    	printf("Name: %s\n",*(*(employee[i].name)));
    	printf("ID: %d\n",*(*employee)[i].id);
    	}
    Unfortunately this doesn't work..

  12. #12
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Is it complete function?

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    I've added in the add_employee function to test if the code worked

    Code:
    int add_employee(Employee **employee,int n_employees)
    {
    	int i;
    	
    	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");
    	getchar();
    	gets( (*employee)[n_employees-1].name);
    	(*employee)[n_employees-1].id=n_employees
    
    	for(i=0;i<n_employees;i++)
    	{
    	printf("Name: %s\n",*(*(employee[i].name)));//This  isn't working
    	printf("ID: %d\n",*(*employee)[i].id));
    	}
    
    	
    	return n_employees;
    }
    Last edited by esmeco; 06-05-2010 at 02:15 AM.

  14. #14
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You are on YOUR OWN now. :P

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,030
    Start fixing what has been pointed out to you. Fix gets and void main.
    Then remember that *(*employee)[i].id) is the same as (*employee[0])[i].id which makes no sense and *(*(employee[i].name)) makes no sense either.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. Passing a double array from main to another function
    By Loctan in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2005, 05:19 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. passing array of structures to function
    By bvnorth in forum C Programming
    Replies: 3
    Last Post: 08-22-2003, 08:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21