Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between typedef int array[3] and typedef int(array)[3]?

Tags:

I recently came across this unorthodox way to define a int array type:

typedef int(array)[3];

At first I thought it was an array of function pointers but then I realized that the * and the () were missing, so by looking into the code I deduced the type array was a int[3] type instead. I normally would declare this type as:

typedef int array[3];

Unless I'm mistaken that they are not the same thing, what is the advantage of doing so in the former way other than to make them look similar to a function pointer?

like image 407
Gabriel Diego Avatar asked Sep 26 '17 10:09

Gabriel Diego


2 Answers

What is the difference between typedef int array[3] and typedef int(array)[3]?

They are the same.


Parentheses could be used when a pointer is being declared, with *, and result in different types. In that case, parentheses could affect the precedence of [] or int. However, this is not your case here.

like image 137
gsamaras Avatar answered Oct 25 '22 17:10

gsamaras


These are both equivalent. The parentheses do not alter the precedence of [] or int in this case.

The tool cdecl helps to confirm this:

  • int (a)[3] gives "declare a as array 3 of int"
  • int a[3] gives "declare a as array 3 of int"
like image 33
Candy Gumdrop Avatar answered Oct 25 '22 16:10

Candy Gumdrop