Home>

I'd like to make a program that "measures the input speed of each key and ranks the speed", but I got the following error for the first time. I don't know the relevant part. ..
Since it goes up to the point where the key input is accepted, I thought that there was probably a cause of an error after/* sort */of the main function, but I do not know. Please tell me who knows the cause!

zsh: bus error ./keytime
Corresponding source code
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/time.h>
#include "conio.h"
struct eachkey {
    int key_num;// key position number
    double keytimes;// For saving each key input time
};
struct eachkey keys [30];
int keyweight [30];// For weighting storage of each key
char keyplace [30] = {'q','w','e','r','t','y','u','i','o','p','a' ,'s','d','f','g','h','j','k','l',';','z','x','c',' v','b','n','m',',','.','/'};
double getETime () {
    struct timeval tv;
    gettimeofday (&tv, NULL);
    return tv.tv_sec + (double) tv.tv_usec * 1e-6;
}
/ * Measure the time when the specified key is pressed * /
double keytime (char c) {
    double start, end, time;
    char getkey;
    int ec;
    printf ("Hand in home position, press spacebar \ n");
    while (1) {
        int space = getch ();
        if (space =='') break;
    }
    start = getETime ();
    printf ("Enter"% c ">>", c);
    while (1) {
        ec = getch ();
        if (ec == c) break;
    }
    printf ("% c \ n", ec);
    end = getETime ();
    time = end --start;
    printf ("key input time:% lf seconds \ n", time);
    return time;
}
int GetRandom (int min, int max) {
    return min + (int) (rand () * (max-min + 1.0)/(1.0 + RAND_MAX));
}
/ * Randomly accept keystrokes + time measurement and weight ranking * /
int main () {
    int i, n, k, h;
    struct eachkey tmp;
    for (i = 0;i<30;i ++) keys [i] .key_num = i;// Key position number setting
    for (i = 0;i<30;i ++) keys [i] .keytimes = -1;// keytimes initialization
    for (i = 0;i<30;i ++) {
        n = GetRandom (0,29);
        while (keys [n] .keytimes! =-1) {// Search for unrecorded keys s
            n = GetRandom (0,29);
        }
        keys [n] .keytimes = keytime (keyplace [n]);
    }
    /*sort*/
    for (k = 0;k<30;k ++) {
        for (h = k + 1;h<30;k ++) {
            if (keys [k] .keytimes>keys [h] .keytimes) {
                tmp = keys [k];
                keys [k] = keys [h];
                keys [h] = tmp;
            }
        }
    }
   /* Weighting * /
    for (i = 0;i<30;i ++) {
        keyweight [keys [i] .key_num] = i;
    }
   /* For checking results * /
    for (i = 0;i<30;i ++) {
        printf ("% d:% d \ n", i, keyboard [i]);
    }
    return 0;
}
Supplementary information (FW/tool version, etc.)

I'm using VScode on my Mac.