gpt4 book ai didi

linux - Linux 内核 4.8.0-53-generic Linux Mint 64 位中 IOCTL 函数的不兼容指针初始化错误

转载 作者:太空狗 更新时间:2023-10-29 11:12:14 24 4
gpt4 key购买 nike

我在使用 Ioctl 命令编写字符设备模块时遇到错误。

static struct file_operations my_fops =
{
.unlocked_ioctl = my_ioctl, error is here. I can not fix this.
};

注意:请忽略我所有的print_k

请帮我解决这个问题。感谢你们所有人。

这是我的代码:

static long my_ioctl(struct file *f,unsigned int cm,unsigned long arg[b]) 
{
int re;
unsigned long arg[3];

switch (cm)
{
case H_ADD:

arg[2] = arg[0] + arg[1];
print_k("Driver:Calculating is complete,Result = %d \n",arg[2]);
break;
case H_SUB:
print_k ("Driver: Start ...\n");
arg[2] = arg[0] - arg[1];
print_k("Driver:Calculating is complete,Result = %d \n",arg[2]);
break;
case H_MULL:
print_k ("Driver: Start ...\n");
arg[2] = arg[0] * arg[1];
print_k("Driver:Calculating is complete,Result = %d \n",arg[2]);
break;
case H_DIV:
print_k ("Driver: Start ...\n");
arg[2] = arg[0] / arg[1];
print_k("Driver:Calculating is complete,Result = %d \n",arg[2]);
break;
default:
print_k ("Driver: I don't have this operation!\n");
re = -Er;
break;
}
return re;
}

static struct file_operations my_fops =
{
.unlocked_ioctl = my_ioctl,
};

最佳答案

函数原型(prototype)中的第三个参数 unsigned long arg[b] 是可疑的。它应该只是unsigned long arg,即使它应该是一个指针。在函数体内将其转换为感兴趣的类型很容易。

..the optional arg argument is passed in the form of an unsigned long, regardless of whether it was given by the user as an integer or a pointer.

( Linux Device Drivers 3, Chapter 6, Section 1 )

此外,在函数体声明一个与参数之一同名的变量是错误的。请为 unsigned long arg[3]; 选择另一个名称。

关于linux - Linux 内核 4.8.0-53-generic Linux Mint 64 位中 IOCTL 函数的不兼容指针初始化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46988664/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com