c - Create a pointer to two-dimensional array -
I need an indicator for a stable 2-dimensional array. How is this done?
Fixed uint8_t l_matrix [10] [20]; Zero test () {uint8_t ** matrix_ptr = l_matrix; // wrong idea}
I will get all types of errors:
- Warning: Assignment with incompatible index type
- subscripted Value here
Here you have the first element of the array one Want to create an indicator
uint8_t (* matrix_ptr) [20] = l_matrix;
With typing, this cleaner looks
typedef uint8_t array_of_20_uint8_t [20]; Array_of_20_uint8_t * matrix_ptr = l_matrix;
Then you can enjoy life again :)
matrix_ptr [0] [1] = ...;
Be careful in C, much confusion is around this.
Edit
Other reviews are here, because comment areas were too few to do there, many options were proposed, but it was not shown how they behaved We do. Here's how they do
uint8_t (* matrix_ptr) [] [20] = l_matrix;
If you fix the error and the operator Then he creates an incomplete array type indicator of a 20% UIT 8_T type array type. Because the indicator is for array of arrays, you have to access it with And because it is an indicator for an incomplete array, you can not do as a shortcut & amp; In the snippet below, type
uint8_t (* matrix_pTR) [] [20] = & amp; nbsp; Amp; L_matrix;
(* matrix_ptr) [0] [1] = ...;
< Code> matrix_peter [0] [0] [1] = ...;
Because indexing is necessary to know the size of element type (index indicator is a pair of integers, so it will not work with incomplete types). Note that this only works in C
, because T []
and T [N]
are compatible types in C ++ Contextual type , and therefore will reject that code, because T []
and T [10]
are different .
The following options do not work at all, because the element type of element, when you see it as a one-dimensional array, not Uint8_t
, but uint8_t [20]
uint8_t * matrix_ptr = l_matrix; // failed
The following is a good choice
uint8_t (* matrix_ptr) [10] [20] = & amp; L_matrix;
You can access it with
(* matrix_ptr) [0] [1] = ...; Matrix_pit [0] [0] [1] = ...; // is still possible
Its advantage protects the size of the external dimension, so you can apply sizeof on it
sizeof (* Matrix_ptr) == Size (uint8_t) * 10 * 20
Another answer is that using the fact that the item is stored processed in an array
uint8_t * matrix_ptr = l_matrix [0];
Now, it formally allows you to access the elements of the first element of only two-dimensional array. That is, hold the following condition
matrix_ptr [0] = ...; // valid matrix_peter [19] = ...; // valid matrix_pret [20] = ...; // Undefined behavior matrix_ptr [10 * 20-1] = ...; // Undefined behavior
You will see that this probably works by 10 * 20-1
, but if you throw in on other name analysis and other aggressive optimization , Then some compiler can make that assumption that can break that code, after saying, I have never encountered a compiler which fails on it (but again, I use that technique in the actual code Not done), and even CFA It is inherent in that technique (warning about its UB'Nation), and if you can not change the array type, then this is the last option to save you :)
Comments
Post a Comment