为什么不能烧焦**在C ++下面的函数的返回类型? [英] Why can't char** be the return type of the following function in C++?

查看:101
本文介绍了为什么不能烧焦**在C ++下面的函数的返回类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中有以下功能:

I have the following function in C++ :

char** f()
{
    char (*v)[10] = new char[5][10];
    return v;
}

Visual Studio 2008中说以下内容:

Visual studio 2008 says the following:

error C2440: 'return' : cannot convert from 'char (*)[10]' to 'char **'

究竟应该怎样返回类型是,为了让此功能工作?

What exactly should the return type to be, in order for this function to work?

推荐答案

的char ** 是不是同一类型为字符(*) 10] 。这两者都是不兼容的类型等字符(*)[10] 不能隐式转换为的char ** 。因此,编译错误。

char** is not the same type as char (*)[10]. Both of these are incompatible types and so char (*)[10] cannot be implicitly converted to char**. Hence the compilation error.

函数的返回值类型看起来非常难看。你必须把它写成:

The return type of the function looks very ugly. You have to write it as:

char (*f())[10]
{
    char (*v)[10] = new char[5][10];
    return v;
}

现在它编译

或者你可以使用的typedef 为:

typedef char carr[10];

carr* f()
{
    char (*v)[10] = new char[5][10];
    return v;
}

Ideone

基本上,字符(* V)[10] 定义一个指针指向大小为10的字符阵列。这是相同的,如下:

Basically, char (*v)[10] defines a pointer to a char array of size 10. It's the same as the following:

 typedef char carr[10]; //carr is a char array of size 10

 carr *v; //v is a pointer to array of size 10

所以你的code变成等价于:

So your code becomes equivalent to this:

carr* f()
{
    carr *v = new carr[5];
    return v;
}


cdecl.org 帮助这里:


  • 的char v [10] 内容申报V作为字符数组10

  • 字符(* V)[10] 内容申报V作为字符指针数组10

  • char v[10] reads as declare v as array 10 of char
  • char (*v)[10] reads as declare v as pointer to array 10 of char

这篇关于为什么不能烧焦**在C ++下面的函数的返回类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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