Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between `extern int (x)[]` and `extern int x[]` in C?

There is declaration extern int (x)[] at the end of this article. Are the parentheses doing anything or they are just for confusion?

My guess would be that with parentheses x is an array of external integers (and that's what the article says), but without those x would be an external array of integers. If this is true, how would the definitions differ? Wouldn't they both be int x[]?

like image 790
Džuris Avatar asked Aug 05 '13 14:08

Džuris


People also ask

What is the difference between extern int and int?

Explanation: extern int fun(); declaration in C is to indicate the existence of a global function and it is defined externally to the current module or in another file. int fun(); declaration in C is to indicate the existence of a function inside the current module or in the same file.

What is extern int in C?

“extern” keyword is used to extend the visibility of function or variable. By default the functions are visible throughout the program, there is no need to declare or define extern functions. It just increase the redundancy. Variables with “extern” keyword are only declared not defined.

What is the difference between global and extern variable in C?

Global variable is a variable that is available throughout the program. An extern variable is also available throughout the program but extern only declares the variable but it doesn't allocate any memory for this variable. It means you can't ise the variable till you define it.

What does extern mean in a function declaration in C?

the extern keyword is used to extend the visibility of variables/functions. Since functions are visible throughout the program by default, the use of extern is not needed in function declarations or definitions. Its use is implicit. When extern is used with a variable, it's only declared, not defined.


2 Answers

Nope, the parens are not doing anything there. The article you link to explains why:

The simplest way to differentiate the two is to look at the placement of the type specifiers. If a type specifier immediately follows a left parenthesis, that parenthesis is the start of a function descriptor and the type is part of a function parameter. Empty parentheses also signify a function. Otherwise the parentheses are grouping (perhaps unnecessarily, but it's better to have too many than too few) the expression.

Also note that "array of external ints" and "external array of ints" are the exact same thing -- the "extern-ness" always goes to the object being declared, which in this case is an array. Personally I think the first way to describe it is technically inaccurate and simply confusing.

like image 185
Jon Avatar answered Oct 15 '22 05:10

Jon


The paranthesis are of no significance in that case. Normally, you use paranthesis for either function-pointers or to mark stronger binding, as all Operators have a different Level of binding.
The extern keyword marks, that this is just a declaration, but the actual definition of this array will happen somwhere else.

like image 28
bash.d Avatar answered Oct 15 '22 05:10

bash.d