指针类型的赋值。
不同的编译器,对于不同类型间的指针变量进行赋值的编译检查是不一样的,有的报警告,有的报错误。
例如:
int main()
{
char a[3][6]={"hello", "world"};
char *p;
p=a;
printf("%c\n", *p ); //输出h
return 0;
}
在devC++工具下编译通过,报警告: [Warning] assignment from incompatible pointer type
在VC6工具下,编译出错报错误:error C2440: '=' : cannot convert from 'char [3][6]' to 'char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
warning: initialization from incompatible pointer type 分析
在字符驱动中,这行代码报了警告信息:
warning: initialization from incompatible pointer type
static ssize_t s3c2440_key_read(struct file *filp, char __user *buf, ssize_t count, loff_t *ppos);
经分析是因为函数声明与函数的原型不符,将其中的:
ssize_t count
改成:
size_t count
就可以了。
同样static void s3c2440_key_release(struct inode *inode, struct file *filp);
将其中的:
void
改成:
int
就可以了。
不同的编译器,对于不同类型间的指针变量进行赋值的编译检查是不一样的,有的报警告,有的报错误,如以下代码:
int main()在devC++工具下,编译通过,报警告: [Warning] assignment from incompatible pointer type
在VC6工具下,编译出错,报错误:error C2440: '=' : cannot convert from 'char [3][6]' to 'char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast (大意为类型转换错误,需要强制转换)
如果确信以上代码就是编者想要的,想去除警告或是错误,可以通过强制转换来完成:
int main()再到两个工具上进行编译,可以通过了!
附:以上是想通过一个一维指针来访问一个二维数组,因为二维数组是连续存放数据的,所以,可以通过一维指针来遍历到二维数组中的所有元素。但如果是想通过指针来逐行访问二维数组的数据,则应该采用数组指针来编写代码:
#include<stdio.h>