C语言里的 [Warning] assignment from incompatible pointer type 是什么意思啊

如题所述

指针类型的赋值。

不同的编译器,对于不同类型间的指针变量进行赋值的编译检查是不一样的,有的报警告,有的报错误。

例如:

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

就可以了。

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-10-02

不同的编译器,对于不同类型间的指针变量进行赋值的编译检查是不一样的,有的报警告,有的报错误,如以下代码:

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 (大意为类型转换错误,需要强制转换)

如果确信以上代码就是编者想要的,想去除警告或是错误,可以通过强制转换来完成:

int main()
{
    char a[3][6]={"hello", "world"};
    char *p;
    p=(char *)a; //只需要改这里,强制转换成与p的类型相一致
    printf("%c\n", *p ); //输出h
    return 0;
}

再到两个工具上进行编译,可以通过了!


附:以上是想通过一个一维指针来访问一个二维数组,因为二维数组是连续存放数据的,所以,可以通过一维指针来遍历到二维数组中的所有元素。但如果是想通过指针来逐行访问二维数组的数据,则应该采用数组指针来编写代码:

#include<stdio.h>

int main()
{
int i;
    char a[3][6]={"hello", "world"};
    char (*p)[6];
    p=a; //可以直接赋值,不会有错
for( i=0;i<3;i++ )
printf("%s\n", p++ ); //输出一行,并将p指向下一行
    return 0;
}

第2个回答  推荐于2017-09-26
subscripted value is neither array nor pointer

下标值不是数组或者指针。。。

invalid lvalue in assignment

无效左值。。。本回答被提问者采纳
相似回答