I wanted to write quick sort but my program have an error. In first in my program had infinite loop in main cycle in qsort function.Than i added left++; right--; and there was an exception. Please review my code and help me solve this problem.
#include <iostream>
using namespace std;
typedef int (*CFT) (const void*, const void*);
struct User
{
    const char* name;
    int dept;
};
void print_id(User* v, int sz);
int cmp2(const void* a, const void* b);
void qsort(void* base, int n, size_t sz, CFT cmp);
User heads[] = {
    "g", 1,
    "d", 3,
    "s", 5,
    "c", 2,
    "t", 4,
    "h", 6,
    "y", 7,
    "z", 8,
};
int main() {
    const int SIZE = 8;
    print_id(heads, SIZE);
    qsort(heads, SIZE, sizeof(User), cmp2);
    cout << endl << endl;
    print_id(heads, SIZE);
    return 0;
}
void print_id(User* v, int sz) {
    for (int i = 0; i < sz; i++)
    {
        cout << v[i].name << "  " << v[i].dept << endl;
    }
}
int cmp2(const void* a, const void* b) {
    int aa = ((User*)a)->dept;
    int bb = ((User*)b)->dept;
    return aa > bb;
}
void qsort(void* base, int n, size_t sz, CFT cmp) {
    char* b = static_cast<char*> (base);
    char* lg = b;
    char* rg = b + n * sz;
    char* left = lg;
    char* right = rg;
    char* control = b + (n / 2) * sz;
    do
    {
        while (cmp(control, left) && left < rg) left += sz;
        while (cmp(right, control) && right > lg) right -= sz;
        if (left <= right) {
            for (int k = 0; k < sz; k++) {
                char tmp = left[k];
                left[k] = right[k];
                right[k] = tmp; // exception write access violation
            }
            left++;
            right--;
        }
    } while (left <= right);
    if (lg < right) qsort(lg, right - lg, sz, cmp);
    if (left < rg) qsort(left, rg - left, sz, cmp);
}
I dont now how templates work so please dont use them in answers.
 
     
    