简体   繁体   中英

C error : assignment to expression with array type

I have developed a C code for my module but when I try to compile it, an error occurs.

This is the part of program where the error exist:

ssize_t exer_read(struct file *pfile, char __user *buffer, size_t length, loff_t *offset) {
    struct file *f = pfile->private_data;
    enum { MAX_BUF_SIZE = 4096 };
    size_t buf_size = 0;
    char *buf = NULL;
    ssize_t total = 0;
    ssize_t rc = 0;

    struct input_event  ev[buf_size];
    int yalv;

    /* Allocate temporary buffer. */
    if (length) {
        buf_size = min_t(size_t, MAX_BUF_SIZE, length);
        ev = kmalloc(buf_size, GFP_KERNEL);
        if (ev == NULL) {
            return -ENOMEM;
        }
    }

And this is the error:

exer_simple_char_drv.c:77:12: error: assignment to expression with array type
         ev = kmalloc(buf_size, GFP_KERNEL);
            ^

I don't know how to solve this. Could anyone help me out please. Thanks

If you want to allocate memory for ev dynamically, as in:

ev = kmalloc(buf_size, GFP_KERNEL);

then you want ev to be a pointer, not an array with automatic storage duration:

struct input_event *ev;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM