Monday 15 June 2015

c - Global integer array with No dimension -


When we define a global array with no dimensions, then it shows output to what it is 16.

  #include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; Int AR []; Int main (int argc, char * argv []) {arr [1] = 16; Printf ("% d \ n", arrival [1]); System ("pause"); Return 0; }   

And even sizeof (arr) does not work. Why?

int arr []; is a temporary definition

Section 6.9.2, paragraph 2 says:

An identifier declaration for an object in which without the initiator The file is scope, and constitutes a temporary definition , with the non-storage class specifier or the storage-category specifier stabilized If one translation unit has one or more floating definitions for an identifier , And that identifier in the translation unit If there is no external definition for the purpose, the behavior is actually such that the translation unit includes the file scope declaration of that identifier composite type, 0 With an initial equal to

and illustrates example 2 in paragraph 5 of that section. :

If at the end of the translation unit

  int i [];   

The array i is still an incomplete type, due to the underlying trigger it is an element, which is set to zero on the program startup.

Then at the end of the translation unit , your array is arr type int [1] . Before the end, its type is incomplete, so sizeof does not work, because in main , array type is still incomplete.

Code> ARR [1] implements the mandatory behavior, because arr has only one element.

No comments:

Post a Comment