当前位置: 技术问答>linux和unix
关于在linux下驱动全局变量赋值问题!!!
来源: 互联网 发布时间:2016-11-15
本文导语: 我在linux下编了一个fifo模块驱动,定义全局变量 struct fifo { struct cdev dev; char buff[MAX_BUF]; int len; struct semaphore sem; wait_queue_head_t wq,rq; }; 然后给在read函数里给定义的全局变量fifo的成员buff赋值...
我在linux下编了一个fifo模块驱动,定义全局变量
struct fifo
{
struct cdev dev;
char buff[MAX_BUF];
int len;
struct semaphore sem;
wait_queue_head_t wq,rq;
};
然后给在read函数里给定义的全局变量fifo的成员buff赋值,用fifo->buff = "asdfddg";
它报错incompatible types in assignment;可是memcpy(fifo->buff, "asdfasf", 8);他就对,为什么??
附程序:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
MODULE_LICENSE("GPL");
#define DEV_CNT 2
#define MAX_BUF 1024
char *modname="mm";
struct fifo
{
struct cdev dev;
char buff[MAX_BUF];
int len;
struct semaphore sem;
wait_queue_head_t wq,rq;
};
struct fifo *fifo;
struct file_operations ops={0},ops1={},ops0={};
dev_t devid;
struct class *cls;
int mm_open (struct inode *inode, struct file *file)
{
int minor=iminor(inode);
if(minor==0)
file->f_op=&ops0;
else
file->f_op=&ops1;
return 0;
}
ssize_t mm_read0(struct file *file, char __user *buff, size_t size, loff_t *off)
{
int count=size;
//fifo->buff = "asdfddg";
memcpy(fifo->buff, "asdfasf", 8); //就是这个地方
fifo->len = 8;
// DECLARE_WAITQUEUE(wait,current);
// add_wait_queue(&fifo->rq,&wait);
down_interruptible(&fifo->sem);
while(fifo->len==0)
{
//set_current_state(TASK_INTERRUPTIBLE);
up(&fifo->sem);
wait_event_interruptible(fifo->rq,0);
// schedule();
if(signal_pending(current))
{
// remove_wait_queue(&fifo->rq,&wait);
return -ERESTARTSYS;
}
down_interruptible(&fifo->sem);
}
if(count>fifo->len)
count=fifo->len;
if(copy_to_user(buff,fifo->buff,count))
{
printk("copy_to_user failedn");
up(&fifo->sem);
//remove_wait_queue(&fifo->rq,&wait);
return -EPERM;
}
else
{
memcpy(fifo->buff,fifo->buff+count,fifo->len-count);
fifo->len-=count;
}
wake_up(&fifo->wq);
//remove_wait_queue(&fifo->rq,&wait);
up(&fifo->sem);
return count;
}
struct fifo
{
struct cdev dev;
char buff[MAX_BUF];
int len;
struct semaphore sem;
wait_queue_head_t wq,rq;
};
然后给在read函数里给定义的全局变量fifo的成员buff赋值,用fifo->buff = "asdfddg";
它报错incompatible types in assignment;可是memcpy(fifo->buff, "asdfasf", 8);他就对,为什么??
附程序:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
MODULE_LICENSE("GPL");
#define DEV_CNT 2
#define MAX_BUF 1024
char *modname="mm";
struct fifo
{
struct cdev dev;
char buff[MAX_BUF];
int len;
struct semaphore sem;
wait_queue_head_t wq,rq;
};
struct fifo *fifo;
struct file_operations ops={0},ops1={},ops0={};
dev_t devid;
struct class *cls;
int mm_open (struct inode *inode, struct file *file)
{
int minor=iminor(inode);
if(minor==0)
file->f_op=&ops0;
else
file->f_op=&ops1;
return 0;
}
ssize_t mm_read0(struct file *file, char __user *buff, size_t size, loff_t *off)
{
int count=size;
//fifo->buff = "asdfddg";
memcpy(fifo->buff, "asdfasf", 8); //就是这个地方
fifo->len = 8;
// DECLARE_WAITQUEUE(wait,current);
// add_wait_queue(&fifo->rq,&wait);
down_interruptible(&fifo->sem);
while(fifo->len==0)
{
//set_current_state(TASK_INTERRUPTIBLE);
up(&fifo->sem);
wait_event_interruptible(fifo->rq,0);
// schedule();
if(signal_pending(current))
{
// remove_wait_queue(&fifo->rq,&wait);
return -ERESTARTSYS;
}
down_interruptible(&fifo->sem);
}
if(count>fifo->len)
count=fifo->len;
if(copy_to_user(buff,fifo->buff,count))
{
printk("copy_to_user failedn");
up(&fifo->sem);
//remove_wait_queue(&fifo->rq,&wait);
return -EPERM;
}
else
{
memcpy(fifo->buff,fifo->buff+count,fifo->len-count);
fifo->len-=count;
}
wake_up(&fifo->wq);
//remove_wait_queue(&fifo->rq,&wait);
up(&fifo->sem);
return count;
}
|
struct fifo
{
struct cdev dev;
char buff[MAX_BUF];
int len;
struct semaphore sem;
wait_queue_head_t wq,rq;
};
然后给在read函数里给定义的全局变量fifo的成员buff赋值,用fifo->buff = "asdfddg";
你的buff如果定义成char ×就对了,原因是数组名还是不一样的,数组名是指向了某块内存区域的指针, (内存区域[MAX_BUF]),不能再将数组名指向别的内存区域。本例中“adfsdaf”是放在常量区域的内存,即
char * pc1 = "a";
char * pc2 = "a";
此时pc1 和pc2的值是一样的,即指向了同一块地址。希望能帮到你。
{
struct cdev dev;
char buff[MAX_BUF];
int len;
struct semaphore sem;
wait_queue_head_t wq,rq;
};
然后给在read函数里给定义的全局变量fifo的成员buff赋值,用fifo->buff = "asdfddg";
你的buff如果定义成char ×就对了,原因是数组名还是不一样的,数组名是指向了某块内存区域的指针, (内存区域[MAX_BUF]),不能再将数组名指向别的内存区域。本例中“adfsdaf”是放在常量区域的内存,即
char * pc1 = "a";
char * pc2 = "a";
此时pc1 和pc2的值是一样的,即指向了同一块地址。希望能帮到你。
|
struct fifo
{
struct cdev dev;
char buff[MAX_BUF];
int len;
struct semaphore sem;
wait_queue_head_t wq,rq;
};
然后给在read函数里给定义的全局变量fifo的成员buff赋值,用fifo->buff = "asdfddg";
/* Wenxy comment: 请打好C主言基础, char buff[MAX_BUF] = "asdfddg"; 就是C++也需要重载=运算符才能这样赋值 */
它报错incompatible types in assignment;可是memcpy(fifo->buff, "asdfasf", 8);他就对,为什么??/* Wenxy comment: 这样做才对 */
|
you are a genius, you cannot assign a char * to char x[], you know that. please read more basic books about c