2

Can anyone please let me know the difference between below two c statements in terms of initialization, scope of table and any other.

NOTE:Both are global variables.

unsigned int *table[100] = {NULL};

static unsigned int *table[100] = {NULL};
pa1
  • 778
  • 3
  • 11
  • 26
  • Please be aware that `... = {NULL}` *only initialises the array's 1st element* to `NULL`. *All other elements get initialised to `0`, Both not necessarily need to be the same depending on the C implementation in use. – alk Sep 23 '16 at 05:54
  • @P.J.Meisch: A duplicate by the title, but not by the content, I feel. – alk Sep 23 '16 at 05:56
  • Referring my previous comment: http://stackoverflow.com/q/9894013/694576 – alk Sep 23 '16 at 06:08

3 Answers3

1
  1. table is an array of pointers of type unsingned int in both the declarations.
  2. The difference is with static the visibility of the array is restricted only to the file in which you are declaring this array.

The link will help

What does "static" mean?

Community
  • 1
  • 1
Gopi
  • 19,784
  • 4
  • 24
  • 36
0

Similarity:

  1. Both are array of 100 integer pointers.
  2. Even if you don't initialize, both will be initialized to NULL as they are declared global.
  3. Both will be stored into Data section.

Difference:

  1. Scope: First one will have global scope which will be accessed anywhere from your program. Second one will have file scope means you can not access those pointers form other file.

Note that if you declared both in the same file then static declaration will get highest preference. i.e. Assigning any value to table pointer will get static initialization.

Nilesh Vora
  • 191
  • 1
  • 3
  • 14
  • 1
    Please see my comment on the OP: http://stackoverflow.com/questions/39653161/difference-between-below-two-c-statements#comment66609677_39653161 – alk Sep 23 '16 at 05:55
0
unsigned int *table[100] = {NULL};

table is an array of pointer to unsingned int and initialize entire array elements to NULL.

static unsigned int *table[100] = {NULL};//declared as static means  initialized only once

table is an array of pointer to static unsingned int and initialize entire array elements to NULL.

msc
  • 33,420
  • 29
  • 119
  • 214
  • Please see my comment on the OP: http://stackoverflow.com/questions/39653161/difference-between-below-two-c-statements#comment66609677_39653161 – alk Sep 23 '16 at 05:55
  • @alk sir but remaining elements are implicitly NULL. am I right or wrong? – msc Sep 23 '16 at 06:01
  • Not necessarily. Please have a look at link I just added as (another) comment to the OP. – alk Sep 23 '16 at 06:09