0

I have a 3 dimensional array and want to create a pointer to the 2nd dimension.

uint32_t m0[4096][256][8];
uint32_t (*m1)[256][8] = &m0[0];
uint32_t *m2 = m1[2];      //<- warning: initialization from incompatible pointer type

// do something with m2[0], m2[1].... m2[7]

Obviously there is something with the above code. What is it?

3 Answers 3

2

m1[2] is *(m1+2).
Type of *(m1+2) is the same as the type of *m1, which is unit32_t [256][8].

For that reason, the following line is wrong.

uint32_t* m2 = m1[2];

What you need to use is:

uint32_t* m2 = (*m1)[2];
Sign up to request clarification or add additional context in comments.

5 Comments

fixed the line, made no difference, same error message
@poby, not sure why. It works for me on my desktop and online at ideone.com/pHYTZz.
This is weird! It actually works only if the m2[0] = 10; line is included. If I don't do an assignation, I get the above error.
@poby, the error is probably about an unused variable, but that's only a guess.
@R.Sahu, yeah you're right, I didn't notice the error was different.
0

your 2nd dimension is not uint32_t * it's simply m[n], or exactly as you've declared m1. m1[n] is uint32_t[8]

Comments

0

That's not possible because the second dimension's values aren't stored next to each other in memory.

1 Comment

From the accepted answer I take that @poby means a flattened array of the last two dimensions. My answer isn't relevant

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.