简体   繁体   English

在C ++中创建一个指向3维数组的指针

[英]Create a pointer to a 3-dimensional array in C++

I've looked at this similar question , but its not working. 我已经看过这个类似的问题 ,但是它不起作用。

Externally, in Filter.h I have 在外部,在Filter.h中,我有

struct test{
    unsigned char arr[3][8192][8192];
}

I have one of these structs initialized, and my code works properly if I use: 我初始化了以下结构之一,并且如果使用以下代码,我的代码也可以正常工作:

initialized_test_struct -> arr[2][54][343]

However, I want to cache a pointer to this array: 但是,我想缓存一个指向此数组的指针:

unsigned char (*new_ptr)[8192][8192] = &(initialized_test_struct -> arr)
assert initialized_test_struct -> arr[2][54][343] == new_ptr[2][54][343]

But when I try this, I get: 但是当我尝试这样做时,我得到:

cannot convert 'unsigned char ( )[3][8192][8192]' to 'unsigned char ( )[8192][8192]' in initialization 无法在初始化时将'unsigned char( )[3] [8192] [8192]'转换为'unsigned char( )[8192] [8192]'

When I try: 当我尝试:

unsigned char (*colors)[3][8192][8192] = &(input -> color);

I get the wrong data type (when being used): 我得到了错误的数据类型(使用时):

error: invalid operands of types 'unsigned char [8192]' and 'char' to binary 'operator*' 错误:类型为'unsigned char [8192]'和'char'的类型为二进制'operator *'的无效操作数

How can I pull this off? 我怎样才能做到这一点?

unsigned char (*new_ptr)[8192][8192] = &(initialized_test_struct -> arr);

should be 应该

unsigned char new_ptr[3][8192][8192] = initialized_test_struct -> arr;

But this is really bad C++, this is better if you use C++11: 但这确实是糟糕的C ++,如果您使用C ++ 11,则更好:

auto new_ptr = initialized_test_struct -> arr;

And I don't know about the specifics of your problem, but a class like a std::vector could give you better usability and be easier to deal with. 而且我不知道您的问题的具体细节,但是像std::vector这样的类可以为您提供更好的可用性,并且更易于处理。

By the way, an array is a pointer already, this is why you don't have to use & . 顺便说一句,数组已经是一个指针,这就是为什么您不必使用& You could want to create a pointer to that array (why?), in that case use: 您可能想创建一个指向该数组的指针(为什么?),在这种情况下,请使用:

auto *new_ptr = &initialized_test_struct -> arr;

and: 和:

unsigned char *new_ptr[3][8192][8192] = &initialized_test_struct -> arr;

and: 和:

assert initialized_test_struct -> arr[2][54][343] == (*new_ptr)[2][54][343]

This should work: 这应该工作:

#include <iostream>

struct test{
    unsigned char arr[3][8192][8192];
};

int main()
{
    test initialized_test_struct;
    unsigned char (*new_ptr)[8192][8192] = initialized_test_struct.arr;
    return 0;
}

Whenever you use an array variable in an expression it is converted to a pointer to the type of the array elements. 每当在表达式中使用数组变量时,它都会转换为指向数组元素类型的指针。 For example, 例如,

int a[3] = {1,2,3};
int* b = a; // this is ok

However, if we do 但是,如果我们这样做

int a[2][1] = {{1}, {2}};
int* b = a; // this will fail, rhs has type int(*)[1], not int*

We would have to do 我们必须做

int a[2][1] = {{1}, {2}};
int (*b)[1] = a; // OK!

If you have a C++11-compatible compiler, you could simply do 如果您具有兼容C ++ 11的编译器,则只需执行

auto new_ptr =  initialized_test_struct.arr;

The compiler takes care of the type-deduction for you and replaces auto with the correct type. 编译器会为您处理类型推导,并将auto替换为正确的类型。

auto new_ptr = initialized_test_struct -> arr;

Was the only thing that I could get to work. 这是我唯一可以工作的东西。 In order for this to work, I also had to add the -std=c++11 flag, as it is part of c++11 为了使其正常工作,我还必须添加-std = c ++ 11标志,因为它是c ++ 11的一部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM