无法将数组转换为指针 [英] Cannot cast array to pointer

查看:40
本文介绍了无法将数组转换为指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下来源:

#include <iostream>
using namespace std;

void main(int j)
{
    char arr[10][10];
    char** ptr;
    ptr = arr;
}

当我使用 VS2010 编译它时,我收到此错误:

when I compile it using VS2010 I get this error:

error : a value of type "char (*)[10]" cannot be assigned to an entity of type "char **"

我认为 C++ 中的数组只是指针.所以 char[][] 也可以是 char**.我做错了什么?

I thought arrays in c++ were just pointers. So a char[][] could also be char**. What am I doing wrong?

推荐答案

char[10][10]char**char (*)[10] 都是不同的类型.但是,第一个不能转换成第二个,它可以转换成第三个.

The types char[10][10] and char** and char (*)[10] are all different types. However, the first one cannot convert into the second one, it can convert into the third one.

试试这个:

char arr[10][10];
char (*ptr)[10];
ptr = arr; //ok

它会起作用,因为正如我所说的,char[10][10] 类型的对象可以转换为 char (*)[10] 类型的对象.它们是兼​​容的类型.

It will work, because as I said object of type char[10][10] can convert into an object of type char (*)[10]. They're compatible types.

这篇关于无法将数组转换为指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆