Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using typedef for an array to declare a new type

Tags:

c

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?

like image 872
user2410592 Avatar asked May 22 '13 17:05

user2410592


Video Answer


1 Answers

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])
like image 160
Daniel Fischer Avatar answered Sep 19 '22 11:09

Daniel Fischer