使用typedef为数组声明一个新类型

时间:2022-02-17 21:31:02

I know how to use typedef in order to define a new type (label).

我知道如何使用typedef来定义新类型(标签)。

For instance, typedef unsigned char int8 means you can use "int8" to declare variables of type unsigned char.

例如,typedef unsigned char int8意味着您可以使用“int8”来声明unsigned char类型的变量。

However, I can't understand the meaning of the following statment:

但是,我无法理解以下声明的含义:

typedef unsigned char array[10]

Does that mean array is of type unsigned char[10]?

这是否意味着数组的类型是unsigned char [10]?

In other part of code, this type was used as a function argument:

在代码的其他部分,此类型用作函数参数:

int fct_foo(array* arr)

Is there anyone who is familiar with this statement?

有没有人熟悉这个陈述?

2 个解决方案

#1


29  

Does that mean array is of type unsigned char[10]?

这是否意味着数组的类型是unsigned char [10]?

Replace "of" with "another name for the" and you have a 100% correct statement. A typedef introduces a new name for a type.

将“of”替换为“另一个名称”,并且您有100%正确的声明。 typedef为类型引入了新名称。

typedef unsigned char array[10];

declares array as another name for the type unsigned char[10], array of 10 unsigned char.

声明数组为unsigned char [10]类型的另一个名称,10个unsigned char数组。

int fct_foo(array* arr)

says fct_foo is a function that takes a pointer to an array of 10 unsigned char as an argument and returns an int.

fct_foo是一个函数,它将一个指向10个unsigned char数组的指针作为参数并返回一个int。

Without the typedef, that would be written as

没有typedef,那就写成了

int fct_foo(unsigned char (*arr)[10])

#2


4  

What that does is it makes a datatype called array that is a fixed length array of 10 unsigned char objects in size.

它的作用是使一个名为array的数据类型是一个固定长度的数组,其大小为10个unsigned char对象。

Here is a similar SO question that was asking how to do a fixed length array and that typedef format is explained in more depth.

这是一个类似的SO问题,询问如何进行固定长度数组,并更深入地解释typedef格式。

#1


29  

Does that mean array is of type unsigned char[10]?

这是否意味着数组的类型是unsigned char [10]?

Replace "of" with "another name for the" and you have a 100% correct statement. A typedef introduces a new name for a type.

将“of”替换为“另一个名称”,并且您有100%正确的声明。 typedef为类型引入了新名称。

typedef unsigned char array[10];

declares array as another name for the type unsigned char[10], array of 10 unsigned char.

声明数组为unsigned char [10]类型的另一个名称,10个unsigned char数组。

int fct_foo(array* arr)

says fct_foo is a function that takes a pointer to an array of 10 unsigned char as an argument and returns an int.

fct_foo是一个函数,它将一个指向10个unsigned char数组的指针作为参数并返回一个int。

Without the typedef, that would be written as

没有typedef,那就写成了

int fct_foo(unsigned char (*arr)[10])

#2


4  

What that does is it makes a datatype called array that is a fixed length array of 10 unsigned char objects in size.

它的作用是使一个名为array的数据类型是一个固定长度的数组,其大小为10个unsigned char对象。

Here is a similar SO question that was asking how to do a fixed length array and that typedef format is explained in more depth.

这是一个类似的SO问题,询问如何进行固定长度数组,并更深入地解释typedef格式。