gpt4 book ai didi

c - C 中的 typedef、数组和指针

转载 作者:太空宇宙 更新时间:2023-11-04 00:36:14 25 4
gpt4 key购买 nike

我正在研究用 C 语言编写的代码。

以下部分我不清楚:

typedef uint8_t data_t[4][4];

typedef struct {
data_t *data;
...
} my_struct;

我不明白的是,data 的类型是什么?

另外,我想给这个变量赋值。比如代码中有:

my_struct st;
st.data = (int8_t *)array

其中数组定义为 int8_t *array

我不明白这个分配是如何工作的,谁能解释清楚吗?

最后,是否可以为我的 data_t 变量赋值,而无需在我的结构中将其声明为指针?

最佳答案

The thing that I don't understand is, what is the type of data?

data 的类型是一个指向二维数组的指针。即 uint8_t(*data)[4][4]

参见 C right-left rule用于破译 C 声明。

Plus, I want to assign values to this variable st.data = (int8_t *)array.

在这种情况下,array 必须与 uint8_t[4][4] 数组具有相同的布局。由于数组在 C 中是连续的,因此 array 必须至少有 4 * 4 个 int8_t 类型的元素。

您必须将 array 转换为 (uint8_t*) 的事实首先意味着 array 具有不同的类型,这可能会导致麻烦。

请注意,这只是一个指针赋值,而不是 array 的按元素复制。

is it possible to assign values to my data_t variable without declaring it as a pointer in my structure?

如果 data 不是指针,则可以,即将其声明为 data_t data;。然后使用 memcpy 复制到其中。

关于c - C 中的 typedef、数组和指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34904376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com