Q: Function with structure as argument producing error: free(): invalid pointer: 0x00007efd47b I've made an addDepartment function that takes a structure as an argument. When I enter input to initialize the "dept[counter].departmentHead" at the bottom of the function, it triggers the error message. 
I'm copying the logic from another code I wrote using classes instead of structures and that one works fine so I'm really not sure why this one isn't working. Tried messing with the index to make sure I wasn't going over the size of the array but that doesn't seem to fix the issue. 
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>

using namespace std;

struct Department{
    string departmentName;
    string departmentHead;
    int departmentID;
    double departmentSalary;
};

//...

Department addDepartment(Department dept[3]){
    int repeat=0;
    int counter=0;
    if (counter>2){
        cout<<"The array is full, you can not add any more Departments."<<endl;
    }
    else{
        cout << "Please Enter Department Details:"<<endl;
        cout << "Department ID : ";
        cin >> dept[counter].departmentID;
        for(int x=0; x<3; x++){
            for (int y=x+1; y<3; y++){
                if(dept[x].departmentID==dept[y].departmentID)
                    repeat++;
            }
        }
        if(repeat!=0)
            cout<<"Value must be unique!"<<endl;
        else{
            cout << "Department Name : ";
            cin >> dept[counter].departmentName;
            cout << "Head of Department : ";
            cin >> dept[counter].departmentHead;
            counter++;
        }
    }
}

//...

int main()
{
    Employee emp[5];
    Department dept[3];
    initialID(emp,dept,0);
    initialID(emp,dept,1);
    int response;
    while(response!=6){
        displayMenu();
        cout<< "Please make a selection : \n";
        cin >> response;
        while((response!=1)&&(response!=2)&&(response!=3)&&(response!=4)&&(response!=5)&&(response!=6)){
            cout<< "Please enter a valid choice (1 - 6): ";
            cin >> response;
        }
        if(response==1){
            addDepartment(dept);
        }
        else if(response==2){
            //addEmployee(emp,dept);
        }
        else if(response==3){

        }
        else if(response==4){

        }
        else if(response==5){
            //salaryReport(dept);
        }
    }
        cout << "Thank you, goodbye.";  
}


A: One problem that I see is that you are creating an array of 3 Department objects in main and assuming that you have 5 elements in initialID.
Change main to create an array of 5 Department objects.
int main()
{
    Employee emp[5];
    Department dept[5];

    ...


A: Why it breaks.
The addDepartment function never actually returns a department. When the function exits, the space where the return newly created Department would be is left uninitialized. This causes undefined behavior. The compiler tries to destruct the Department object like it normally would, but because it was never initialized, free gets called on garbage (causing the error). 
We can fix this by adding a line to addDepartment returning the actual department:
Department addDepartment(Department dept[3]){
    int repeat=0;
    int counter=0;
    if (counter>2){
        cout<<"The array is full, you can not add any more Departments."<<endl;
    }
    else{
        cout << "Please Enter Department Details:"<<endl;
        cout << "Department ID : ";
        cin >> dept[counter].departmentID;
        for(int x=0; x<3; x++){
            for (int y=x+1; y<3; y++){
                if(dept[x].departmentID==dept[y].departmentID)
                    repeat++;
            }
        }
        if(repeat!=0)
            cout<<"Value must be unique!"<<endl;
        else{
            cout << "Department Name : ";
            cin >> dept[counter].departmentName;
            cout << "Head of Department : ";
            cin >> dept[counter].departmentHead;
            counter++;
        }
    }
    return /* some department */;
}

Alternatively, you could make addDepartment void. 
Other considerations. Don't pass raw C arrays to functions. It doesn't do what you intend. 
If you want to pass a copy of an array, pass a std::array, which will be copied automatically:
Department addDepartment(std::array<Department, 3> dept); 

If want to access the elements of an existing array, pass a pointer:
Department addDepartment(Department* dept, int count); 

