Reputation: 183
I know how to use typedef in order to define a new type (label).
For instance, typedef unsigned char int8
means you can use "int8" to declare variables of type 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]?
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?
Upvotes: 14
Views: 32799
Reputation: 183858
Does that mean array is of type
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.
typedef unsigned char array[10];
declares array
as another name for the type unsigned char[10]
, array of 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
.
Without the typedef
, that would be written as
int fct_foo(unsigned char (*arr)[10])
Upvotes: 33
Reputation: 127543
What that does is it makes a datatype called array
that is a fixed length array of 10 unsigned char
objects in size.
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.
Upvotes: 4