Search code examples
clinuxcompilationtimeoutselect-function

C programming error with FD_SET


I have an issue with my code, and I can't find out what the problem is as i'm new to select();
Could anyone please tell me what's the issue from?


run_fct.c: In function ‘run’:
run_fct.c:22: error: invalid type argument of ‘->’ (have ‘fd_set’)
cc1: warnings being treated as errors
run_fct.c:11: error: unused variable ‘timeout’
make: *** [run_fct.o] Error 1

FD_SET(0, fds);

void run(t_coord* piece, t_env* env, t_used_piece* rand_piece)
{
    char            arrows[2000];
    int             number;
    int             nread;
    int             ret;
    fd_set          fds;
    struct timeval  timeout;
    int             test;

    test = 0;
    (void)env;
    number = 0;
    set_keypress();
    init_pieces(piece);
    while (1)
    {
        FD_ZERO(&fds);
        FD_SET(0, fds);
        ret = select(1, &fds, 0, 0, 0);
        if (test == 0)
        {
            get_piece(rand_piece);
            number = rand_piece->piece;
            print_piece(env, piece, number);
        }
        if (ret > 0)
        {
            if (FD_ISSET(0, &fds))
            {
                nread = read(0, arrows, 3);
                if (arrows[0] == 27 && arrows[1] == '[' && arrows[2] == 'A')
                {
                    printf("A = UP\n");
                    piece[number].ay = piece[number].ay + 1;
                }
                else if (arrows[0] == 27 && arrows[1] == '[' && arrows[2] == 'B')
                {
                    piece[number].ay = piece[number].ay + 2;
                }
                else if (arrows[0] == 27 && arrows[1] == '[' && arrows[2] == 'C')
                {
                    piece[number].ax = piece[number].ax + 1;
                    piece[number].ay = piece[number].ay + 1;
                }
                else if (arrows[0] == 27 && arrows[1] == '[' && arrows[2] == 'D')
                {
                    piece[number].ax = piece[number].ax - 1;
                    piece[number].ay = piece[number].ay + 1;
                }
                else
                {
                    piece[number].ay = piece[number].ay + 1;
                }
            }
        }
        test = 1;
    }
    reset_keypress();
}

Solution

  • Perhaps you mean FD_SET(0, &fds); (you forgot the & before fds).

    And I do hope you know that using select has some limitations. ppoll, poll or pselect might be better.