[英]typedef, arrays and pointers in C
I am studying a code written in C language. 我正在学习用C语言编写的代码。
The following part isn't clear for me: 以下部分对我来说尚不清楚:
typedef uint8_t data_t[4][4];
typedef struct {
data_t *data;
...
} my_struct;
The thing that I don't understand is, what is the type of data
? 我不明白的是,什么是
data
类型?
Plus, I want to assign values to this variable. 另外,我想为这个变量赋值。 For example, in the code there is:
例如,在代码中有:
my_struct st;
st.data = (int8_t *)array
where array is defined as int8_t *array
. 其中array定义为
int8_t *array
。
I don't understand how this assignation works, could someone explain it clearly? 我不了解此分配的工作原理,有人可以清楚地解释吗?
To finish, is it possible to assign values to my data_t
variable without declaring it as a pointer in my structure? 最后,是否可以将值
data_t
变量而无需在结构中将其声明为指针?
The thing that I don't understand is, what is the type of data?
我不明白的是,数据类型是什么?
The type of data
is a pointer to a two-dimensional array. data
的类型是指向二维数组的指针。 That is uint8_t(*data)[4][4]
. 那就是
uint8_t(*data)[4][4]
。
See C right-left rule for deciphering C declarations. 有关解密C声明,请参见C左右规则 。
Plus, I want to assign values to this variable
st.data = (int8_t *)array
.另外,我想为该变量
st.data = (int8_t *)array
。
In this case array
must have the same layout as uint8_t[4][4]
array. 在这种情况下,
array
必须与uint8_t[4][4]
数组具有相同的布局。 Since arrays are contiguous in C
, that array
must have at least 4 * 4 elements of type int8_t
. 由于数组在
C
是连续的,因此该array
必须至少具有4 * 4个int8_t
类型的int8_t
。
The fact that you have to cast the array
with (uint8_t*)
first implies that array
has a different type and that may cause trouble. 首先必须使用
(uint8_t*)
array
的事实意味着该array
具有不同的类型,这可能会引起麻烦。
Note that this is a pointer assignment only, not an element-wise copy of array
. 请注意,这只是一个指针分配,而不是
array
逐元素副本。
is it possible to assign values to my data_t variable without declaring it as a pointer in my structure?
是否可以将值赋给data_t变量而无需在结构中将其声明为指针?
It is possible if data
is not a pointer, ie declare it as data_t data;
如果
data
不是指针,则可能将其声明为data_t data;
. 。 And then copy into it using
memcpy
. 然后使用
memcpy
将其复制到其中。
This declaration 这个宣言
typedef uint8_t data_t[4][4];
declares name data_t
as an alias for type uint8_t [4][4]
. 声明名称
data_t
作为uint8_t [4][4]
类型的别名。
Thus in the structure definition 因此在结构定义中
typedef struct {
data_t *data;
...
} my_struct;
member data is a pointer of type data_t *
that is the same as uint8_t ( * )[4][4]
. 成员数据是
data_t *
类型的指针,与uint8_t ( * )[4][4]
。
So it is a pointer to a two-dimensional array of pointers to objects of type `uint8_t 因此,它是指向二维数组的指针,该数组指向`uint8_t类型的对象
Arrays do not have the assignment operator. 数组没有赋值运算符。 You have to copy elements of one array into another.
您必须将一个数组的元素复制到另一个数组中。
If for example you have another one-dimensional array of pointers like 例如,如果您还有另一个一维指针数组,例如
uint8_t *array[4];
you could write 你可以写
my_struct st;
st.data = malloc( sizeof( data_t ) );
memcpy( st.( *data )[0], array, 4 * sizeof( uint8_t * ) );
Take into account that as the data
is a pointer you have at first to allocate memory where you are going to copy objects of type uint8_t *
. 考虑到由于
data
是指针,您首先必须在要复制uint8_t *
类型的对象的地方分配内存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.