C 中带有数组的 Typedef
Typedef with arrays in C
您好,我是 C 编程的新手。 python 长期使用后,C 似乎无限难(也无限有趣)!
我无法解决的问题是将 typedef
用于数组,尤其是二维数组。
typedef double vector_t[10];
据我了解,这有助于我们使用 vector_t
初始化一个包含 10 个元素的 double
数组。那么初始化 vector_t[10]
会初始化一个 [10][10]
的数组吗?
另外,如果我初始化 vector_t[5]
会怎样?
typedef vector_t second_t[5];
如果我使用 second_t
会怎样?二维数组是 [10][5]
的数组还是 [5][10]
的数组?
如果你使用
second_t v;
这和
完全一样
vector_t v[5];
这与
完全一样
double v[5][10]; /* NOT double[10][5] */
当您扩展 typedef
时,假设您要用 typedef
之后的任何内容替换其定义中 typedef
的名称:
typedef something t[size];
t x;
/* subst [t := x] into the typedef definition */
something x[size];
second_t v;
/* [second_t := v] in definition */
vector_t v[5];
/* [vector_t := v[5]] in definition */
double v[5][10];
typedef int (*unary_op)(int); /* pointers to functions int => int */
typedef int (*consumer)(unary_op); /* pointers to functions (int => int) => int */
consumer f;
/* [consumer := f] in definition */
int (*f)(unary_op);
/* [unary_op := ] (replace unary_op with "", because there's no name) in definition.
We must respect parentheses */
int (*f)(int (*)(int));
// Something like int func(unary_op op) { return op(5); }
As far as I understand this helps us use vector_t to initialise an array of doubles with 10 elements.
这不太正确。准确地说,这个 typedef 定义了一个自定义类型,它允许 声明 一个数组,比如 vector_t example[N];
真正的维度将是 N + 1
(因为 vector_t
已经假定单个元素本身就是一维数组)。说 initialise 意味着你用一些数据填充内存。在这种特殊情况下,您可以说 memset(my_vec, 0, sizeof(my_vec));
来将数组归零。如果你有一个更简单的变量,比如 int a;
,那么你可以 初始化 它,比如 a = 1;
.
如果您声明 vector_t another_example[10][10]
,这实际上会给您一个 3 维数组 - 10 x 10 x 10。
what will happen if I use second_t? will the 2d array be Array[10][5] or Array[5][10]??
因此,正如您从我的 post 开头所理解的那样,在后一种情况下,声明 second_t Array[10][5]
和 second_t Array[5][10]
都不会给出二维数组。
实际上,这将是 4 维数组,因为 second_t
将单个元素定义为已经是二维数组。
出于教育目的,我建议您从
之类的内容开始
#include <stdio.h>
#include <stdint.h>
typedef uint8_t vector_t[10];
为了能够构造更复杂的类型,然后声明数组(比如,变量将被称为 array
),最后,做类似 printf("The size is %lu bytes\n", sizeof(array));
的事情以查看以字节为单位的总大小。然后很容易看出总大小是多少,因为最基本的类型是 uint8_t
(1 字节)。
您好,我是 C 编程的新手。 python 长期使用后,C 似乎无限难(也无限有趣)!
我无法解决的问题是将 typedef
用于数组,尤其是二维数组。
typedef double vector_t[10];
据我了解,这有助于我们使用 vector_t
初始化一个包含 10 个元素的 double
数组。那么初始化 vector_t[10]
会初始化一个 [10][10]
的数组吗?
另外,如果我初始化 vector_t[5]
会怎样?
typedef vector_t second_t[5];
如果我使用 second_t
会怎样?二维数组是 [10][5]
的数组还是 [5][10]
的数组?
如果你使用
second_t v;
这和
完全一样vector_t v[5];
这与
完全一样double v[5][10]; /* NOT double[10][5] */
当您扩展 typedef
时,假设您要用 typedef
之后的任何内容替换其定义中 typedef
的名称:
typedef something t[size];
t x;
/* subst [t := x] into the typedef definition */
something x[size];
second_t v;
/* [second_t := v] in definition */
vector_t v[5];
/* [vector_t := v[5]] in definition */
double v[5][10];
typedef int (*unary_op)(int); /* pointers to functions int => int */
typedef int (*consumer)(unary_op); /* pointers to functions (int => int) => int */
consumer f;
/* [consumer := f] in definition */
int (*f)(unary_op);
/* [unary_op := ] (replace unary_op with "", because there's no name) in definition.
We must respect parentheses */
int (*f)(int (*)(int));
// Something like int func(unary_op op) { return op(5); }
As far as I understand this helps us use vector_t to initialise an array of doubles with 10 elements.
这不太正确。准确地说,这个 typedef 定义了一个自定义类型,它允许 声明 一个数组,比如 vector_t example[N];
真正的维度将是 N + 1
(因为 vector_t
已经假定单个元素本身就是一维数组)。说 initialise 意味着你用一些数据填充内存。在这种特殊情况下,您可以说 memset(my_vec, 0, sizeof(my_vec));
来将数组归零。如果你有一个更简单的变量,比如 int a;
,那么你可以 初始化 它,比如 a = 1;
.
如果您声明 vector_t another_example[10][10]
,这实际上会给您一个 3 维数组 - 10 x 10 x 10。
what will happen if I use second_t? will the 2d array be Array[10][5] or Array[5][10]??
因此,正如您从我的 post 开头所理解的那样,在后一种情况下,声明 second_t Array[10][5]
和 second_t Array[5][10]
都不会给出二维数组。
实际上,这将是 4 维数组,因为 second_t
将单个元素定义为已经是二维数组。
出于教育目的,我建议您从
之类的内容开始#include <stdio.h>
#include <stdint.h>
typedef uint8_t vector_t[10];
为了能够构造更复杂的类型,然后声明数组(比如,变量将被称为 array
),最后,做类似 printf("The size is %lu bytes\n", sizeof(array));
的事情以查看以字节为单位的总大小。然后很容易看出总大小是多少,因为最基本的类型是 uint8_t
(1 字节)。