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?

2
  • You should try the typedef approach proposed at the link you posted. Commented Mar 27, 2015 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]]; Commented Mar 27, 2015 at 23:38

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I just saw your answer. This auto new_ptr = initialized_test_struct.arr is basically what I did!
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]

4 Comments

This didn't work. I added c++11 to my flags, and I'm not getting any more errors, but it doesn't actually work.
Additionally, this is unclear: "auto *new_ptr = &initialized_test_struct -> arr;" and: "unsigned char *new_ptr[3][8192][8192] = &initialized_test_struct -> arr;" have an inherent conflict?
I think it's the ->arr that's the error , instead of .arr
-> arr would be dereferencing a pointer, and &initialized_test_struct is a pointer to the struct.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.