0

I've looked at this similar question, but its not working.

Externally, in Filter.h I have

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

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*’

How can I pull this off?

Community
  • 1
  • 1
Nathan Bellowe
  • 4,662
  • 3
  • 15
  • 16
  • You should try the typedef approach proposed at the link you posted. – Antonio Mar 27 '15 at 23:33
  • You should try the `typedef` approach proposed at the link you posted. `typedef unsigned char array_of_8192x8192_uchar_t[8192][8192]];` – Antonio Mar 27 '15 at 23:38

3 Answers3

1
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:

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.

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]
coyotte508
  • 9,175
  • 6
  • 44
  • 63
1

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

auto new_ptr =  initialized_test_struct.arr;

The compiler takes care of the type-deduction for you and replaces auto with the correct type.

jensa
  • 2,792
  • 2
  • 21
  • 36
0
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

Nathan Bellowe
  • 4,662
  • 3
  • 15
  • 16