传递指针二维数组C ++ [英] Passing pointer to 2D array c++

查看:164
本文介绍了传递指针二维数组C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个问题相当长的时间 - 我有固定大小的二维数组作为一个类成员

I'm having this problem for quite a long time - I have fixed sized 2D array as a class member.

class myClass
{ 
public:
        void getpointeM(...??????...);
        double * retpointM();

private:

   double M[3][3];

};

int main()
{
     myClass moo;
     double *A[3][3];

     moo.getpointM( A );  ???
     A = moo.retpointM(); ???

} 

我想通过指针 M 矩阵之外。这也可能是很简单,但我就是无法找到&放适当的组合; *

I'd like to pass pointer to M matrix outside. It's probably very simple, but I just can't find the proper combination of & and * etc.

感谢您的帮助。

推荐答案

双* A [3] [3]; 是一个2维数组双* 秒。你想双(* A)[3] [3];

double *A[3][3]; is a 2-dimensional array of double *s. You want double (*A)[3][3]; .

然后,请注意, A * A ** A 都具有相同的地址,只是不同的类型。

Then, note that A and *A and **A all have the same address, just different types.

制作的typedef可以简化事情:

Making a typedef can simplify things:

typedef double d3x3[3][3];

这是C ++,你应该通过引用传递变量,不是指针:

This being C++, you should pass the variable by reference, not pointer:

void getpointeM( d3x3 &matrix );

现在,你不需要使用类型名称括号,编译器可以确保你传递正确的大小的数组。

Now you don't need to use parens in type names, and the compiler makes sure you're passing an array of the correct size.

这篇关于传递指针二维数组C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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